-->

Symfony 2.3 - Custom Fatal Errors

2019-07-16 06:06发布

问题:

I've got a problem with Symfony 2.3, I can't get it to work to get a custom function that handles the fatal errors. The Symfony exceptions work fine, like a 404 etc. but a fatal error not.

This is what I've got after searching the internet, I've a created a parent controller in the TestBundle. The other default controller extends the TestingSomeThingController. Se the files below for the code. But when I deleted the ; at the end of the line in DefaultController it's shows the standard fatal error page instead of the "yaay" I put in the code. Does anyone have got an answer for this question? Thanks!

TestingSomeThingController

<?php

namespace TestingSomeThing\TestBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller as BaseController;
use Symfony\Component\HttpFoundation\Response;

class TestingSomeThingController extends BaseController
{
    public function __construct()
    {
        set_error_handler(array($this, 'sdfg'));
        register_shutdown_function(array($this, 'sdfg'));
    }

    public function sdfg()
    {
        $error = error_get_last();
        if(isset($error)) {
            echo 'yaay';
            die();
        }
    }
}

DefaultController

<?php

namespace TestingSomeThing\TestBundle\Controller;

use TestingSomeThing\TestBundle\Controller\TestingSomeThingController;

class DefaultController extends TestingSomeThingController
{
    public function indexAction($name)
    {
        return $this->render('TestingSomeThingBundle:Default:index.html.twig', array('name' => $name))
    }
}

回答1:

A fatal error is by definition unrecoverable.

In simple terms it means that no more processing can continue.

There is no way to handle it and nor should you want to try either. The whole point is to tell you that something fatal occurred and no more processing can take place.

fa·tal /ˈfātl/ Adjective (1) Causing death. (2) Leading to failure or disaster.

This is true in almost all languages (including English) and is not something specific to Symfony or PHP - you simply don't recover from something fatal.