SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Escape function

  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Escape function

    Hm I am stumbled...Does anyone know how I can break out of a function in this way?

    PHP Code:
    function someFunc()
    {
         
    $this->callOtherFunc()
         
    doStuff

    Is there a way to implement the $this->callOtherFunc() so it escapes someFunc on certain conditions without making a check inside the someFunc?
    I could implement this:
    PHP Code:
    $return $this->callOtherFunc()
    if( 
    $return === 'return')
    {
      return 
    true;
    } else {
     
    doStuff

    but just curious if i could escape the function from within the other.

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're basically looking for the "return" through multiple stack frames. No, there's no such feature in php. You can achieve something similar using exceptions:

    PHP Code:
    function first($param) {
        echo 
    "first starts\n";
        
    second($param);
        echo 
    "first continues\n\n";
    }

    function 
    second($param) {
        if(
    $param == "skip")
            throw new 
    Exception;
    }

    try {
        
    first('normal');
        
    first('skip');
    } catch(
    Exception $e){}

    echo 
    "end\n"

  3. #3
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pestilence_gr View Post
    Hm I am stumbled...Does anyone know how I can break out of a function in this way?

    PHP Code:
    function someFunc()
    {
         
    $this->callOtherFunc()
         
    doStuff

    Is there a way to implement the $this->callOtherFunc() so it escapes someFunc on certain conditions without making a check inside the someFunc?
    I could implement this:
    PHP Code:
    $return $this->callOtherFunc()
    if( 
    $return === 'return')
    {
      return 
    true;
    } else {
     
    doStuff

    but just curious if i could escape the function from within the other.
    If you're running PHP 5 you could do it by throwing an exception in callOtherFunc(). Whether that's appropriate, depends on what you're actually doing.

    Another question you might ask yourself is whether you want to Separate Query from Modifier. The callOtherFunc() method looks like a candidate for this refactoring, but it's hard to tell with your non-specific naming.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually I am working on a framework and I am implementing a short kind of Observer pattern to inject plugins on the core of the system.
    I wanted this feature to escape certain functions from within the plugins for Instance:
    PHP Code:
    class DB {

    function 
    query()
    {
        
    $this->eventHandler->trigger('beforeQuery');
        ...
        
    querystuff

    This could for instance call a cache plugin, which would need to escape the query function on certain conditions (fetch object from cache, if its there we don't need the rest of the query function, if it isn't proceed to the normal operations of the function).
    As i mentioned earlier I could implement a check on the return result of the plugin and further decide what to do inside the function. Thats why I asked maybe we could bypass the check and not fill the application with exceptions or checks
    Thanks allot for the replies

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you're looking for exceptions.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •