0

I find a weird thing in Zend Framework 1.12.

In action function, I new a object which doen't exist. Code as below:

 public function signupAction ()
    {

        $tbuser = new mmmm();//mmmm does not exist, so there's exception here
    }

But it doesn't turn to the ErrorController.

I tried below code, it works. It turned to the ErrorController, and showed Application Error.

public function signupAction ()
{
    throw new Exception('pppp');
}

What's wrong? Need I config something else?

1 Answer 1

2

Because "class not found" is falta error, not Exception

So Zend does NOT catch it when calls $controller -> dispatch().

See this block please (Zend_Controller_Dispatcher_Standard):

try {
    $controller->dispatch($action);
} catch (Exception $e) {
    //...
}

To avoid this error, you can use function class_exists to check class has been defined or not before call it.

See this link: class_exists

Update:

By default, falta error will cause current php script to be shut down.

So you need (1)customize error handler and (2)change Falta Error to Exception and it can be catched by ErrorController

Like this (in index.php):

register_shutdown_function('__fatalHandler');

function __fatalHandler() {
    $error = error_get_last();
    if ( $error !== NULL && $error['type'] === E_ERROR ) {
        $frontController = Zend_Controller_Front::getInstance();
        $request = $frontController->getRequest();
        $response = $frontController->getResponse();
        $response->setException(new Exception('Falta error:' . $error['message'],$error['type']));

        ob_clean();// clean response buffer
        // dispatch
        $frontController->dispatch($request, $response);
    }
}

Ref: Zend framework - error page for PHP fatal errors

Sign up to request clarification or add additional context in comments.

1 Comment

I want that whatever error occurs, I can get the error message and echo it, how can I do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.