Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Ruturn from nested functions within class.

Posted on 2006-11-03
5
Medium Priority
?
217 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
[X]
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
  • 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

661 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