2

A child script terminates the parent script because it has exit;

Since it is a third party extension, I need to avoid any core hack. Would it be possible somehow to ignore the exit of the child script from parent script. I am calling its controller and a method from an external script.

parent.php

<?php

require "child.php";

?>

child.php

<?php
does something;
exit;
?>

Update

Any alternative solution would be fine as long as we dont modify the child script.

3
  • poor design ofthe third party script to use exit. Commented Sep 8, 2014 at 20:56
  • That is not a child script, it is the same script. Both include and require are essentially shorthand for "put the content of this file here". Simply remove the exit from child.php and the excution will continue normally. Commented Sep 8, 2014 at 20:57
  • they developed the script for ajax interface and json output. do you still think that using exit is a poor design in the end of json output? Commented Sep 8, 2014 at 21:23

5 Answers 5

5

Is it possible to ignore exit from included script in PHP?

No.

exit terminates execution of the script regardless from where it is called.

As noted in sjagr's answer, there are alternatives to using exit.

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

5 Comments

That could be an alternative. But as the question stands, the answer is still no.
I think my answer addresses the problem correctly, please tell me what's wrong with this solution.
+1 this is the exact correct answer to the scope that OP has defined and question they asked. @PrestonS A thread could work, but exit still isn't ignored in that scenario, it's just handled explicitly.
@sjagr it is ignored in a sense as the child thread exits allowing the main thread to continue running once the child thread terminates.
Sorry, my question was not clear. By saying "ignore the exit somehow", I mean any alternative solution would be an answer for me that could be done from the parent scripts without editing the child script.
2

If you do in fact end up editing the core files, then it is possible to use return inside of the "child script." From the PHP docs:

If called from the global scope, then execution of the current script file is ended. If the current script file was included or required, then control is passed back to the calling file. Furthermore, if the current script file was included, then the value given to return will be returned as the value of the include call. If return is called from within the main script file, then script execution ends. If the current script file was named by the auto_prepend_file or auto_append_file configuration options in php.ini, then that script file's execution is ended.

However, there is no way to prevent the parent script from preventing a child script from killing the process if it has an exit statement. Unfortunately you cannot override this functionality.

1 Comment

Unfortunately I've realized that my answer does not fit OP's question scope and requirements, however I'm leaving my answer up since it can solve the problem with some modification of the scope and may serve useful to someone else in the future
1

You might be able to run child.php in a thread. Use join to wait until that thread finishes before continuing with the main thread. This way, calling exit in child.php will terminate the child thread and the main thread will continue.

class myThread extends Thread {
  public function run(){
    include "child.php";
    //Call methods from child.php here
  }
}

$thread = new myThread();
$thread->start();
$thread->join();

1 Comment

I should try this as well.
0

Thank you so much everyone for answering the question. Finally I came up with the following alternative idea. I am not sure if it is similar to any design pattern. The following example does not get terminated from loop although the child.php has exit.

parent.php

<?php
require "temp.php";

for($i=1;$i<10;$i++){
    file_get_contents("http://url/temp.php?var=$i");
}

?>

temp.php

<?php
$var = $_GET['var'];
// execute 
require "child.php";
$testController = new TestController();
$testController->method($var);
?>

Comments

-1

Yes. In your child.php you can use a return to return the control back to the parent.php. Otherwise if child.php isn't included, it will continue with its normal operation, which is exit.

<?php
does something;
return;
exit;
?>

If you want to check whether a file was included or run directly, you could this answer. So in that case, make a check in the child file, if it isn't included, goto the exit command, otherwise continue with the rest of the code.

2 Comments

I need to avoid any core hack
@putvande added the link to another answer at the bottom of my post, which might be useful to you.

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.