Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Ruturn from nested functions within class.

Posted on 2006-11-03
5
212 Views
Last Modified: 2013-12-12
I am currently writting a class. Within this class values will be returned and processed elsewhere. Within the class there is also subset of values that may be set before 'return' is called. Before every possible return a set of things will be processed. Currently I have this defined next to each condition that will cause the function to end. Instead of repeating the code throughout the class I would like to include it in a function, but the functions may be nested. How can I break out of the entire class instead of just the nested function.

For example

class ThisClass {
      private $ReadyToReturn = false;
      
      public function CalledFunction ($Vars) {
            //Do Some Stuff.
            if ($SomeCondition) {
                  $this->Subfunction1();
                  $this->Subfunction2();
            } else {
                  $this->Subfunction2();
            }
            
            if ($SomeCondition) {
                  $this->Subfunction2();
            }
            
            $this->IsReadyToReturn();
            
            $this->Subfunction1();
            $this->Subfunction1();
            $this->Subfunction1();
            
            $this->IsReadyToReturn();
            
            $this->Subfunction1();
            $this->IsReadyToReturn();
            
      }
      
      private function Subfunction1 () {
            if ($SomeCondition) {
                  $this->IsReadyToReturn();
            }
      }
      
      public function Subfunction2 () {
            if ($SomeCondition) {
                  $this->IsReadyToReturn();
            }
            if ($SomeCondition) {
                  $this->Subfunction1();
            }
      }
      
      private function IsReadyToReturn () {
            if ($this->ReadyToReturn) {
                  //Do some conditional stuff
                  return ($Array);  //I WANT THIS LINE TO RETURN BACK TO CALLING SCRIPT NOT RETURNING TO THE CALLING FUNCTION WITHN CLASS
            }
      }
}
?>

I want the commented return to return to the calling script not the previous function. How can this be done? I want to avoid having to keep trak of how many nested functions I am using. Is it even possible to break from a class? Possibly by analyzing debug_backtrace?

Thanks for any help.

  obby
0
Comment
Question by:BobsRe
  • 3
5 Comments
 
LVL 5

Author Comment

by:BobsRe
ID: 17870972
For now I replaced

     private function IsReadyToReturn () {
          if ($this->ReadyToReturn) {
               //Do some conditional stuff
               return ($Array);  //I WANT THIS LINE TO RETURN BACK TO CALLING SCRIPT NOT RETURNING TO THE CALLING FUNCTION WITHN CLASS
          }
     }

with

     private function Return () {
          //Do some conditional stuff
          return ($Array);
          }
     }

And every

$this->IsReadyToReturn();

With

if ($this->ReadyToReturn) {
     $this->Return;
}

it is better but still not ideal.

Any suggestions?
0
 
LVL 5

Author Comment

by:BobsRe
ID: 17871026
In the above comment

if ($this->ReadyToReturn) {
     $this->Return;
}

was to read

if ($this->ReadyToReturn) {
     return ($this->Return);
}
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 17876059
I think your latest setup would be the most likely to work. Keep a boolean flag to state that there is a return value present, then break out any function when the flag is set to true.
I don't see however a clear example where you would need a unknown number of nested functions to be able to return something. There will always be some degree of structure in it, which you should be able to use rather than the current setup.

-r-
0
 
LVL 5

Author Comment

by:BobsRe
ID: 18074359
Thanks for your assistance Roonaan.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Cookie not unsetting 7 27
How is this connection happening? 3 20
error while installing php56 in redhat enterprise linux 20 50
How to fix Datetime in MySQL? 4 48
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question