Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error php

Is there a way to make the code continue (not exit) when you get a fatal error in PHP? For example I get a timeout fatal error and I want whenever it happens to skip this task and the continue with others. In this case the script exits.

like image 274
Granit Avatar asked Jul 17 '09 08:07

Granit


2 Answers

The most simple answer I can give you is this function: http://php.net/manual/en/function.pcntl-fork.php

In more detail, what you can do is:

  • Fork the part of the process you think might or might not cause a fatal error (i.e. the bulk of your code)
  • The script that forks the process should be a very simple script.

For example this is something that I would do with a job queue that I have:

<?php
// ... load stuff regarding the job queue

while ($job = $queue->getJob()) {
    $pid = pcntl_fork();
    switch ($pid) {
        case -1:
            echo "Fork failed";
            break;
        case 0:
            // do your stuff here
            echo "Child finished working";
            break;
        default:
            echo "Waiting for child...";
            pcntl_wait($status);
            // check the status using other pcntl* functions if you want
            break;
    }
}
like image 74
andho Avatar answered Oct 23 '22 00:10

andho


There is a hack using output buffering that will let you log certain fatal errors, but there's no way to continue a script after a fatal error occurs - that's what makes it fatal!

If your script is timing out you can use set_time_limit() to give it more time to execute.

like image 22
Greg Avatar answered Oct 23 '22 01:10

Greg