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?
Throw an exception in funcB that is not handled in funcA
<?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!');
}
?>
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.
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');
?>
Related
I've got a script that runs a custom email obfuscation class's Obfuscate() function on the content before displaying it, as follows:
ob_start(array($obfuscator, "Obfuscate"));
include('header.php');
print($html);
include('footer.php');
ob_end_flush();
That all works great. However, I've completely rewritten my view architecture, so I need to run the email obfuscation from within a class function and return that string (which then gets echoed). I initially rewrote the above as:
ob_start(array($this->obfuscator, "Obfuscate"));
include('header.php');
echo($this->content);
include('footer.php');
$wrappedContent = ob_get_contents();
ob_end_clean();
Unfortunately, the $this->obfuscator->Obfuscate() callback is not being fired. I have since learned that ob_get_contents() does not fire the callback, but have tried ob_get_clean() & ob_get_flush() to no avail as well.
So, how can I get the contents of the buffer after the callback has been fired?
Of course, I was overlooking the fact that the only reason to use the callback on ob_start() was because I wanted to run Obfuscate() on the content before it was flushed, but if I'm getting that content back I don't need to run a callback! So, not using a callback and just running ob_get_clean()'s results through Obfuscate() does what I wanted. Doh!
ob_start();
include('header.php');
echo($this->content);
include('footer.php');
return $this->obfuscator->Obfuscate(ob_get_clean());
Change
ob_end_clean();
with
ob_clean()
Will trigger .
This is the code eg I tried
<?php
class Obfuscate {
public function __construct()
{
}
public function getObfuscate()
{
return "obfuscate";
}
}
class Example
{
public function hello( $obfuscator )
{
ob_start(array( $obfuscator, 'getObfuscate' ));
include('header.php');
echo "Thi is a content ";
include('footer.php');
$wrappedContent = ob_get_contents();
ob_clean();
}
}
$obfuscator = new Obfuscate();
$example = new Example;
$example->hello($obfuscator);
ob_clean();
Is it possible to have return statements inside an included file that is inside a function in PHP?
I am looking to do this as I have lots of functions in separate files and they all have a large chunk of shared code at the top.
As in
function sync() {
include_once file.php;
echo "Test";
}
file.php:
...
return "Something";
At the moment the return something appears to break out of the include_once and not the sync function, is it possible for the included file's return to break out?
Sorry for the slightly odly worked question, hope I made it make sense.
Thanks,
You can return data from included file into calling file via return statement.
include.php
return array("code" => "007", "name => "James Bond");
file.php
$result = include_once "include.php";
var_dump("result);
But you cannot call return $something; and have it as return statement within calling script. return works only within current scope.
EDIT:
I am looking to do this as I have lots
of functions in separate files and
they all have a large chunk of shared
code at the top.
In this case why don't you put this "shared code" into separate functions instead -- that will do the job nicely as one of the purposes of having functions is to reuse your code in different places without writing it again.
return will not work, but you can use the output buffer if you are trying to echo some stuff in your include file and return it somewhere else;
function sync() {
ob_start();
include "file.php";
$output = ob_get_clean();
// now what ever you echoed in the file.php is inside the output variable
return $output;
}
I don't think it works like that. The include does not simply put the code in place, it also evaluates it. So the return means that your 'include' function call will return the value.
see also the part in the manual about this:
Handling Returns: It is possible to
execute a return() statement inside an
included file in order to terminate
processing in that file and return to
the script which called it.
The return statement returns the included file, and does not insert a "return" statement.
The manual has an example (example #5) that shows what 'return' does:
Simplified example:
return.php
<?php
$var = 'PHP';
return $var;
?>
testreturns.php
<?php
$foo = include 'return.php';
echo $foo; // prints 'PHP'
?>
I think you're expecting return to behave more like an exception than a return statement. Take the following code for example:
return.php:
return true;
?>
exception.php:
<?php
throw new exception();
?>
When you execute the following code:
<?php
function testReturn() {
echo 'Executing testReturn()...';
include_once('return.php');
echo 'testReturn() executed normally.';
}
function testException() {
echo 'Executing testException()...';
include_once('exception.php');
echo 'testException() executed normally.';
}
testReturn();
echo "\n\n";
try {
testException();
}
catch (exception $e) {}
?>
...you get the following output as a result:
Executing testReturn()...testReturn() executed normally.
Executing testException()...
If you do use the exception method, make sure to put your function call in a try...catch block - having exceptions flying all over the place is bad for business.
Rock'n'roll like this :
index.php
function foo() {
return (include 'bar.php');
}
print_r(foo());
bar.php
echo "I will call the police";
return array('WAWAWA', 'BABABA');
output
I will call the police
Array
(
[0] => WAWAWA
[1] => BABABA
)
just show me how
like this :
return (include 'bar.php');
Have a good day !
If I have a function that is inside of a Class, and I am returned with "invalid" how can I start back up at the top function?
function test(){
//curl here
//other stuff here
if(strpos($data, 'invalid')){
print "invalid";
//discard and remove
continue ;
}
}
but I get the following error
Fatal error: Cannot break/continue 1 level in
If I am hit with "invalid" I would like to start test() back over..
You probably want to use a recursive function here:
function test(){
//curl here
//other stuff here
if(strpos($data, 'invalid')){
print "invalid";
//discard and remove
return test() ; // restart process
}
}
Alternatively, this may be a (very rare) good use of the goto operator:
function test(){
start:
//curl here
//other stuff here
if(strpos($data, 'invalid')){
print "invalid";
//discard and remove
goto start;
}
}
Note that this will only work in PHP >=5.3.
I'd make use of exceptions, and let the calling scope control repeat invocations of test(). test() performs one job; it shouldn't control when it's asked to perform that job.
(Recursive approaches given in other examples don't really fit the use case [and make me nervous, thanks to languages where recursing too many times makes you run out of stack space eventually; certainly you're filling up your stack for no reason], and though goto will work fine it still gives the function itself too much power.)
function test()
{
//curl here
//other stuff here
if (strpos($data, 'invalid'))
throw new Exception("Data is invalid");
}
function callingFunction()
{
while (true) {
try {
test();
break; // only reached if test() didn't throw
}
catch(Exception $e) {} // if we fall into this, the loop repeats
}
}
You could still apply goto quite cleanly with this approach:
function test()
{
//curl here
//other stuff here
if (strpos($data, 'invalid'))
throw new Exception("Data is invalid");
}
function callingFunction()
{
startTest:
try {
test();
}
catch(Exception $e) {
goto startTest;
}
}
Remove continue; and instead add test();. It's called recursive functions (calling itself).
There are a few ways you can do it, the way my teacher normally suggests is to actually call the function again.
function test(){
//curl here
//other stuff here
if(strpos($data, 'invalid')){
print "invalid";
//discard and remove continue ;
test();
return;
}
}
My answer might not be the best, hope this helps.
Assuming you change the parameters beforehand, you can recursively call the function again with new parameters. Just make sure there will be an edge case where the function will stop recursing. As for that, I'd need to see the real code to tell you.
function test($data){
//curl here
//other stuff here
if(strpos($data, 'invalid')){
print "invalid";
//discard and remove
test($NEW_DATA);
}
}
If you want to break out of the function you're currently in, you should use 'return' instead of 'continue'.
Or if you would like to start the processing over, you should put your logic into a loop.
$fooinstance = new foo();
/*do something*/
exit('bar');
class foo{
__destruct(){
//get the exit message ("bar") to do something with it
}
}
Hello,
I would like to get the exit message to do something with it (for example insert the exit status in a database). Is there a way to do that ?
Thanks
The text exit sends isn't special; it's just text that's output before the script dies.
You could get the text with output buffering, though I'm not sure it'll be useful:
<?php
$fooinstance = new foo();
ob_start();
exit('bar');
class foo{
function __destruct(){
$c = ob_get_contents(); //$c is "bar"
}
}
It would probably be best to wrap the exit instruction in a function that did the appropriate logging.
This is an incorrect way to do things. Why do you need this particular solution?
I have noticed a behavior in PHP that makes sense, but I am unsure how to get around it.
I have a long script, something like this
<?php
if ( file_exists("custom_version_of_this_file.php") ) {
require_once "custom_version_of_this_file.php";
exit;
}
// a bunch of code etc
function x () {
// does something
}
?>
Interestingly, the function x() will get registered with the script BEFORE the require_once() and exit are called, and therefore, firing the exit; statement does not prevent functions in the page from registering. Therefore, if I have a function x() in the require_once() file, the script will crash.
Because of the scenario I am attempting (which is, use the custom file if it exists instead of the original file, which will likely be nearly identical but slightly different), I would like to have the functions in the original (calling) file NOT get registered so that they may exist in the custom file.
Anyone know how to accomplish this?
Thanks
You could use the function_exists function. http://us.php.net/manual/en/function.function-exists.php
if (!function_exists("x")) {
function x()
{
//function contents
}
}
You can wrap your functions in a class and extend it to add custom versions.
this_file.php
class DefaultScripts {
function x () {
echo "base x";
}
function y() {
echo "base y";
}
}
if(file_exists('custom_version_of_this_file.php'))
require_once('custom_version_of_this_file.php');
else
$scripts = new DefaultScripts();
custom_version_of_this_file.php
class CustomScripts extends DefaultScripts {
function x () {
echo "custom x";
}
}
$scripts = new CustomScripts();
Results if file exists
$scripts->x(); // 'custom x'
$scripts->y(); // 'base y'
and if it doesn't
$scripts->x(); // 'base x'
$scripts->y(); // 'base y'