Solved

Ruturn from nested functions within class.

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Maps API and PHP 25 32
Php recording post 4 48
google analytics in wordpress 4 35
Add email address from a web page to outlook 2010 contacts ? 8 28
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

810 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