简体   繁体   中英

PhP - how to catch fatal errors in a static class

I have a static class and I want to be able to treat fatal error. I'm pasting the method below so you understand better what I mean:

public static function getInformationDataArrayWithJsonInput($formConfigType, $application_data_json){

    set_error_handler('myErrorHandler');
    register_shutdown_function('fatalErrorShutdownHandler');

    $ret = [];
    $formConfigs = FormConfig::find(array('form_config_type_id='.$formConfigType, 'order" => "ordination ASC'));

    if($formConfigs->count() == 0)
        return $ret;

    $json = $application_data_json;
    //-----------------------------------------------------
    $jsonObj = json_decode($json);

    foreach ($formConfigs as $field){
        $str = "if(\$jsonObj->".$field->json_property."){";
        $str .= "\$ret['".$field->label."'] = \$jsonObj->".$field->json_property.";";
        $str .= "} else {";
        $str .= "\$ret['".$field->label."']=null;";
        $str.="}";

        try{
            eval($str);
        }
        catch (Exception $e)
        {
            die($e->getMessage());

        }
    }

    return $ret;

}

   function fatalErrorShutdownHandler()
{

    die('fatalErrorShutdownHandler');

    $last_error = error_get_last();
    if ($last_error['type'] === E_ERROR) {
        // fatal error
        myErrorHandler(E_ERROR, $last_error['message'], $last_error['file'], $last_error['line']);
    }
}

function myErrorHandler($errno, $errstr, $errfile, $errline)
{

    die('myErrorHandler');

    if (!(error_reporting() & $errno)) {
        // This error code is not included in error_reporting, so let it fall
        // through to the standard PHP error handler
        return false;
    }

    switch ($errno) {
        case E_USER_ERROR:
            echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
            echo "  Fatal error on line $errline in file $errfile";
            echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
            echo "Aborting...<br />\n";
            exit(1);
            break;

        case E_USER_WARNING:
            echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
            break;

        case E_USER_NOTICE:
            echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
            break;

        default:
            echo "Unknown error type: [$errno] $errstr<br />\n";
            break;
    }

    /* Don't execute PHP internal error handler */
    return true;
}

This method receives a json and a string with the property of which I want to retrieve the value from. myErrorHandler and fatalErrorShutDownHandler are implemented according to this article

Let's say I passed products[1].limits[0].value as the json property and there is no products[1] this will cause a fatal error so I would have to return null. That's my problem, I can't go into the catch to do the proper treatment.

Can you help me?

You don't need to be using eval here, and should avoid its use whenever possible. Variable property names are easy enough to do, and by using intermediate variables you make it easier to read. Then a simple property_exists() does the work for you.

$jsonObj = json_decode($json);

foreach ($formConfigs as $field) {
    $prop = $field->json_property;
    $label = $field->label;
    $ret[$label] = property_exists($jsonObj, $prop) ? $jsonObj->$prop : null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM