PHP-是否可以在_destruct()方法中使用自定义异常处理程序(set_exception_handler)?

PHP-是否可以在_destruct()方法中使用自定义异常处理程序(set_exception_handler)?,php,Php,在类的\uu destruct方法中是否有方法使用自定义异常处理程序而不是默认异常处理程序 例如: function myExceptionHandler($e) { echo "custom exception handler"; if(is_object($e) && method_exists($e,'getMessage')) echo $e->getMessage(); } set_exception_handler('myExce

在类的
\uu destruct
方法中是否有方法使用自定义异常处理程序而不是默认异常处理程序

例如:

function myExceptionHandler($e)
{
    echo "custom exception handler";
    if(is_object($e) && method_exists($e,'getMessage'))
        echo $e->getMessage();
}
set_exception_handler('myExceptionHandler');
class MyClass {
    public function __construct()
    {
        // myExceptionHandler handles this exception
        //throw new Exception("Exception from " . __METHOD__);
    }

    public function doStuff()
    {
        // myExceptionHandler handles this exception
        //throw new Exception("Exception from " . __METHOD__);
    }

    public function __destruct()
    {
        // default exception handler 
        throw new Exception("Exception from " . __METHOD__);
    }

}

$myclass = new MyClass();
$myclass->doStuff();
即使在
\uu destruct
方法中调用了
set\u exception\u处理程序
,默认处理程序仍将使用:

    public function __destruct()
    {
        $callable = function($e)
        {
            echo "custom exception handler".PHP_EOL;
            if(is_object($e) && method_exists($e,'getMessage'))
                echo $e->getMessage();
        };
        set_exception_handler($callable);
        throw new Exception("Exception from " . __METHOD__); // default exception handler
    }

也许是这样的

<?php

class MyException extends Exception {
    public function __construct() {
        parent::__construct('my exception');
    }
}

class MyClass {
    public function __construct()
    {
        // myExceptionHandler handles this exception
        //throw new Exception("Exception from " . __METHOD__);
    }

    public function doStuff()
    {
        // myExceptionHandler handles this exception
        //throw new Exception("Exception from " . __METHOD__);
    }

    public function __destruct()
    {
        // default exception handler 
        throw new MyException();
    }

}

$myclass = new MyClass();
$myclass->doStuff();

大概是这样的吧

<?php

class MyException extends Exception {
    public function __construct() {
        parent::__construct('my exception');
    }
}

class MyClass {
    public function __construct()
    {
        // myExceptionHandler handles this exception
        //throw new Exception("Exception from " . __METHOD__);
    }

    public function doStuff()
    {
        // myExceptionHandler handles this exception
        //throw new Exception("Exception from " . __METHOD__);
    }

    public function __destruct()
    {
        // default exception handler 
        throw new MyException();
    }

}

$myclass = new MyClass();
$myclass->doStuff();
来自

注意:

试图从析构函数(在时间中调用)引发异常 脚本终止)导致致命错误

因此,首先在析构函数中使用异常可能是个坏主意。当脚本完成处理抛出的异常时,可能没有任何代码

也许这段代码最好放在类的
close()
方法中

注意:

试图从析构函数(在时间中调用)引发异常 脚本终止)导致致命错误

因此,首先在析构函数中使用异常可能是个坏主意。当脚本完成处理抛出的异常时,可能没有任何代码


也许这段代码最好放在类的
close()
方法中。

谢谢,是的,我最后做了类似于
close()
方法的事情谢谢,是的,我最后做了类似于
close()
方法的事情。我认为这也不管用(尝试了类似的事情)。根据奈杰尔和手册,这是不可能的(也许是不可取的),我认为这也不管用(尝试过类似的东西)。根据奈杰尔和手册,这是不可能的(也可能是不可取的)