Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to skip fatal error from include file in php?

If I include a file in to php. If there is any fatal error in that php then is there any way to skip that .

<?php
   include "somefile.php";
   echo "OK"; // Is there any way to print this OK  If there is any fatal error on somefile.php
?>

I need to include this somefile.php file. It may return fatal error for some host. I want to skip this file for those host.

Please Advice me.

like image 973
Sarwar Hasan Avatar asked Jun 18 '13 05:06

Sarwar Hasan


People also ask

What is the solution of fatal error in php?

Solution. Look for the undeclared variables as given in the error. If you are using inbuilt functions, ensure that there is no typo and the correct function is called. Check if the spellings are correct.

Which function gives fatal error if file is not included?

You need to use include(). Require(), when used on non-existent file, produces a fatal error and exits the script, so your die() won't happen. Include() only throws warning and then the script continues.

In which PHP version fatal errors become exceptions?

Many fatal and recoverable fatal errors have been converted to exceptions in PHP 7. These error exceptions inherit from the Error class, which itself implements the Throwable interface (the new base interface all exceptions inherit).

What would occur if a fatal error was thrown in your PHP program?

Fatal errors are critical errors - for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP's default behavior is to display them to the user when they take place.


3 Answers

With this, you can define your own continuation function that will take over in case of a fatal error. This uses register_shutdown_function() to intercept the fatal error.

Usage:

function my_continuation_func($filename, $arg2) {
    // On fatal error during include, continue script execution from here.
    // When this function ends, or if another fatal error occurs,
    // the execution will stop.
}

include_try('my_continuation_func', array($filename, $arg2));
$data = include($filename);
$error = include_catch();

If a fatal error occurs (like a parse error), script execution will continue from my_continuation_func(). Otherwise, include_catch() returns true if there was an error during parsing.

Any output (like echo 'something';) from the include() is treated as an error. Unless you enabled output by passing true as the third argument to include_try().

This code automatically takes care of possible working directory changes in the shutdown function.

You can use this for any number of includes, but the second fatal error that occurs cannot be intercepted: the execution will stop.

Functions to be included:

function include_try($cont_func, $cont_param_arr, $output = false) {
    // Setup shutdown function:
    static $run = 0;
    if($run++ === 0) register_shutdown_function('include_shutdown_handler');

    // If output is not allowed, capture it:
    if(!$output) ob_start();
    // Reset error_get_last():
    @user_error('error_get_last mark');
    // Enable shutdown handler and store parameters:
    $params = array($cont_func, $cont_param_arr, $output, getcwd())
    $GLOBALS['_include_shutdown_handler'] = $params;
}

function include_catch() {
    $error_get_last = error_get_last();
    $output = $GLOBALS['_include_shutdown_handler'][2];
    // Disable shutdown handler:
    $GLOBALS['_include_shutdown_handler'] = NULL;
    // Check unauthorized outputs or if an error occured:
    return ($output ? false : ob_get_clean() !== '')
        || $error_get_last['message'] !== 'error_get_last mark';
}

function include_shutdown_handler() {
    $func = $GLOBALS['_include_shutdown_handler'];
    if($func !== NULL) {
        // Cleanup:
        include_catch();
        // Fix potentially wrong working directory:
        chdir($func[3]);
        // Call continuation function:
        call_user_func_array($func[0], $func[1]);
    }
}
like image 150
jlgrall Avatar answered Oct 22 '22 17:10

jlgrall


Fatal means fatal ... There is no way to recover from a fatal error.

like image 39
Orangepill Avatar answered Oct 22 '22 18:10

Orangepill


You can use register_shutdown_function.

<?php
function echoOk()
    {
    echo "OK";
    }

register_shutdown_function(function ()
        {
        $error = error_get_last();
        // to make sure that there is any fatal error
        if (isset($error) &&
            ($error['type'] == E_ERROR
            || $error['type'] == E_PARSE
            || $error['type'] == E_COMPILE_ERROR
            || $error['type'] == E_CORE_ERROR))
            {
            echoOk();
            }
        });

include "somefile.php";

echoOk();

But you can do it only once. Any further fatal error will stop execution.

like image 2
sectus Avatar answered Oct 22 '22 17:10

sectus