Solved

Ruturn from nested functions within class.

Posted on 2006-11-03
5
210 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
change password links 7 72
Animated .jpg? 13 59
mysql update statement effect only some rows 4 26
How can I echo a PHP variable in AJAX function? 7 30
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

912 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now