简体   繁体   English

关闭时没有找到该类

[英]The class is not found from the closure

This is my ErrorHandler class, I register the handlers from the main Application class calling ErrorHandler::init(); 这是我的ErrorHandler类,我从主要的Application类中注册调用ErrorHandler::init();的处理程序ErrorHandler::init();

I don't understand why but the class ErrorResponse is not found in set_error_handler and set_exception_handler . 我不明白为什么但是在set_error_handlerset_exception_handler找不到类ErrorResponse

However the class is found in register_shutdown_function ... that's pretty weird. 但是这个类在register_shutdown_function找到......这很奇怪。

Could you know which can be the reason which the ErrorResponse class is not found in the other handlers? 你能知道在其他处理程序中找不到ErrorResponse类的原因是什么?

<?php

namespace tools\exception;

use tools\core\http\ErrorResponse;

class ErrorHandler
{
    static public function init()
    {
        self::_registerNormalErrorHandler();
        self::_registerExceptionErrorHandler();
        self::_registerFatalErrorHandler();
    }

    static private function _registerNormalErrorHandler()
    {
        set_error_handler(function ($err_code, $err_message, $err_file, $err_line)
        {
                $responseCode = 500;
                ErrorResponse::create(
                    $responseCode,
                    $err_file,
                    $err_message,
                    $err_line,
                    "set_error_handler")->sendJSON();

                die();
        });
    }
    static private function _registerExceptionErrorHandler()
    {
        set_exception_handler(function ($exception)
        {
                $responseCode = $exception->getCode();
                if(0 === $responseCode)
                    $responseCode = 500;

                ErrorResponse::create(
                    $responseCode,
                    $exception->getFile(),
                    $exception->getMessage(),
                    $exception->getLine(),
                    "set_exception_handler")->sendJSON();

                die();
        });
    }
    static private function _registerFatalErrorHandler()
    {
        register_shutdown_function(function ()
        {
            $last_error = error_get_last();
            if( null !== $last_error ) {

                $isError = false;
                switch ($last_error["type"]) {
                    // ...
                }
                if($isError) {
                    ErrorResponse::create(
                        500,
                        $last_error['file'],
                        $last_error['message'],
                        $last_error['line'],
                        $caught)->sendJSON();
                }
            }
        });
    }
}

I fixed it :D, the problem was the Liskov substitution principle it was making the class not found error...I found the Declaration of compatible... (the normal error you get when you don't respect Liskov substitution principle ) in some log. 我修复了它:D,问题是Liskov替换原则它使得类找不到错误...我发现兼容声明...(当你不尊重Liskov替换原则时得到的正常错误)in一些日志。

The ErrorResponse is the child of Response class and both had a factory method called create() to instantiate them. ErrorResponse是Response类的子级,并且都有一个名为create()工厂方法来实例化它们。 I removed the factory method on ErrorResponse and I instantiate it using the normal way new ErrorResponse( ... ) 我删除了ErrorResponse上的工厂方法,并使用正常的方式将其实例化为new ErrorResponse( ... )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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