1
votes

In my current configuration, whenever some PHP file has fatal errors such as syntax errors or calling a function that does not exist, I usually get an error message such like:

Parse error: syntax error, unexpected <whatever> in /path/to/file.php on line XXX

or

Fatal error: Call to undefined function whatever() in /path/to/file.php on line YYY

or the like in the very output.

However, I am using third-party libraries which use a third-party autoloader. Whenever there's a fatal error in any of the autoloaded classes (including parse errors or calling unexisting functions - actually not completely sure about the latter but definitely of the parse error case), I just get a blank page, and not only that: no error is even logged in Apache's error_log file, where usually PHP fatal errors would be logged. So debugging becomes impossible.

I can't stress this enough: this only happens when the fatal error is in some autoloaded file. In every other case (including of course errors in files included via require(), include() and the like), the same errors do show up in the output and in the error_log.

I didn't write the autoloader code, but it's basically like this:

    // no idea why this line, but I don't think it's relevant:
    ini_set('unserialize_callback_func', 'spl_autoload_call');

    spl_autoload_register(array('My_Autoloader', 'autoload'), true);

    class My_Autoloader {
        static function autoload($classname) {
            $filename = //.... computes $filename from $classname
            require_once($filename);
        }
    }

There must be a way to have the autoloader throw errors the same way they would be thrown (and handled) if the errors were not in an autoloaded file, right?

How do I get that?

3
Made an answer from my comment - JustOnUnderMillions
But this Fatal error: Call to undefined function whatever() has nothing to do with syntax errors. - JustOnUnderMillions
With PHP7 most fatal errors have been converted into catchable exceptions. trowski.com/2015/06/24/throwable-exceptions-and-errors-in-php7 - Charlotte Dunois
You can statically analyze all files to eliminate parse error concerns from the command line, like on Unixoid (in your code root directory): find -type f -exec php -l {} \; > /dev/null - bishop

3 Answers

0
votes

Syntax Error Check only on Command Line

With php -l somefile.php from PHP shell_exec('php -l /fullpath/to/somefile.php')

but you have to analyse the respone string for errors.

Normal response No syntax errors detected in somefile.php

In PHP <= 5.0.4 there was php.net/manual/en/function.php-check-syntax.php

Here a fatal error catch that works:

register_shutdown_function(function(){
    $err=error_get_last();
    if($err['type']===1){
       /*you got an fatal error do something, write it to an file*/
       #file_put_contents(var_export($err,true),'myfatalerror.log');
    }
});

Hope that helps:)

0
votes

The only way that the code would behave as you suggest is if the third party code is overriding the error reporting. That is usually considered good practice for production systems, but it should be logging the error.

That your third party code is causing such errors gives me pause to wonder about its quality, but we'll ignore that for now.

PHP's built in mechanisms will handle the reporting (to the browser) and the logging (to file). Non fatal errors can be managed by your own code after calling set_error_handler() however fatal errors are not handed off via this route. It is possible to trap and handle fatal errors in your own code using register_shutdown_function(). But start by checking your log files.

If, as you say, both error logging and error reporting are disabled, then stop using this third party code - it is toxic.

-2
votes

Use php exception so you can call your file into

function inverse($x) {
    if (!$x) {
        throw new Exception('Division par zéro.');
    }
    return 1/$x;
}

try {
    echo inverse(5) . "\n";
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Exception reçue : ',  $e->getMessage(), "\n";
}