6

Within PHP, if I have one function that calls another function; is there any way to get the called function to exit out of the caller function without killing the entire script?

For instance, let's say I have some code something like:

<?php
function funcA() {
    funcB();
    echo 'Hello, we finished funcB';
}

function funcB() {
    echo 'This is funcB';
}
?>
<p>This is some text. After this text, I'm going to call funcA.</p>
<p><?php funcA(); ?></p>
<p>This is more text after funcA ran.</p>

Unfortunately, if I find something inside of funcB that makes me want to stop funcA from finishing, I seem to have to exit the entire PHP script. Is there any way around this?

I understand that I could write something into funcA() to check for a result from funcB(), but, in my case, I have no control over the contents of funcA(); I only have control over the contents of funcB().

To make this example a little more concrete; in this particular instance, I am working with WordPress. I am hooking into the get_template_part() function, and trying to stop WordPress from actually requiring/including the file through the locate_template() function that's called after my hook is executed.

Does anyone have any advice?

1
  • Id also suggest asking what you want to do on WP. Maybe there is a function or a wp_filter you can use instead. I'd ask here and on the wordpress.stackexchange.com site. Commented Apr 21, 2011 at 17:20

4 Answers 4

1

Throw an exception in funcB that is not handled in funcA

Sign up to request clarification or add additional context in comments.

1 Comment

Although abusing exceptions for flow control is evil.
0
<?php
  function funcA() {
     try
     {
        funcB();
        echo 'Hello, we finished funcB';
     }
     catch (Exception $e) 
     {
        //Do something if funcB causes an error, or just swallow the exception
     }
  }

  function funcB() {
     echo 'This is funcB';
     //if you want to leave funcB and stop funcA doing anything else, just
     //do something like:
     throw new Exception('Bang!');
  }
?>

2 Comments

Thanks for the try, but, as I said in my original post, I have no control over the code that makes up funcA(), so I wouldn't be able to put the try/catch statement inside of that function.
Then you could wrap your call to funcA() in the exception handler.
0

The only way I see is using exceptions:

function funcA() {
    funcB();
    echo 'Hello, we finished funcB';
}

function funcB() {
   throw new Exception;
   echo 'This is funcB';
}
?>
<p>This is some text. After this text, I'm going to call funcA.</p>
<p><?php  try { funcA(); } catch (Exception $e) {} ?></p>
<p>This is more text after funcA ran.</p>

Ugly, but it works in PHP5.

1 Comment

Thanks for the tip. I should be able to make this work for me. I was kind of hoping to accomplish this without having to edit any of the template files (which is where I'd have to put the try/catch statement), but this is good information for the future.
0

Maybe...

It's not an solution, but you could hook another function that gets called when exit() is requested "register_shutdown_function('shutdown');". And to somehow have this get things continue again or complete to your satifaction.

<?php
function shutdown()
{
    // This is our shutdown function, in 
    // here we can do any last operations
    // before the script is complete.

    echo 'Script executed with success', PHP_EOL;
}

register_shutdown_function('shutdown');
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.