Error and exception handling in php7

前端 未结 1 1017
粉色の甜心
粉色の甜心 2021-01-11 15:00

Recently moved to php7. The following error occurs:

argument 1 passed to MyClass\\Throwable::exceptionHandler() must be an instance of Exception, instance of         


        
相关标签:
1条回答
  • 2021-01-11 15:25

    Errors and Exceptions both extend Throwable however Errors are not extended from Exception.

    Therefore, your ExceptionHandler must accept an object of Type Throwable in order to accept Errors.

    Simplest fix is this, though you may want to rename $exception to make it clear.

    namespace MyClass;
    
    class Throwable
    {
        public function exceptionHandler(\Throwable $exception)
        {
            //logic here
        }
    }
    

    Note: The new Error class should not be confussed with an ErrorException which has classicly been used as a device for turning PHP 5 errors into Exception objects with symantic meaning.

    http://php.net/manual/en/class.error.php

    0 讨论(0)
提交回复
热议问题