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.

codelogn
  • 353
  • 1
  • 3
  • 13
  • poor design ofthe third party script to use exit. –  Sep 08 '14 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. – Sammitch Sep 08 '14 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? – codelogn Sep 08 '14 at 21:23

5 Answers5

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.

Community
  • 1
  • 1
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • That could be an alternative. But as the question stands, the answer is still *no*. – Jason McCreary Sep 08 '14 at 21:07
  • I think my answer addresses the problem correctly, please tell me what's wrong with [this solution](http://stackoverflow.com/a/25733118/2464634). – Preston S Sep 08 '14 at 21:09
  • 1
    +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 Sep 08 '14 at 21:09
  • @sjagr it is ignored in a sense as the child thread exits allowing the main thread to continue running once the child thread terminates. – Preston S Sep 08 '14 at 21:16
  • 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. – codelogn Sep 08 '14 at 21:43
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.

sjagr
  • 15,983
  • 5
  • 40
  • 67
  • 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 – sjagr Sep 08 '14 at 20:59
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();
Preston S
  • 2,751
  • 24
  • 37
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);
?>
codelogn
  • 353
  • 1
  • 3
  • 13
-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.

Community
  • 1
  • 1
Chique
  • 735
  • 3
  • 15