Solved

Ruturn from nested functions within class.

Posted on 2006-11-03
5
209 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for your assistance Roonaan.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

771 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

10 Experts available now in Live!

Get 1:1 Help Now