5

我有方法,在这个方法中可能会发生致命错误,为了捕捉这个错误我做了这个

class a {


    function shutDownFunction() { 
        $error = error_get_last();
        if ($error['type'] == 1) {
            echo "this is fatal error";
        } 
    }


    function terribleFunction () {
        register_shutdown_function(array($this,'shutdownFunction'));


        // here is code, wich may causes fatal error

    }


}

好的,这明白了,但我需要从terribleFunctionto传递参数shutDownFunction。这个怎么做?

4

3 回答 3

13

首先,您需要指定shutDownFunction应该接受一个参数。

function shutDownFunction($var)

然后你可以register_shutdown_function这样打电话

register_shutdown_function(array($this, 'shutdownFunction'), $myVar);

文档在这里,评论中有示例。

于 2012-12-04T12:40:20.193 回答
0

您可以查看register_shutdown_function文档。还有第二个可选参数parameter传递给您的shutDownFunction(). 这个函数你可以这样定义:

function shutDownFunction($args) {
   //do whatever you want with $args
}
于 2012-12-04T12:41:31.610 回答
0

您应该只使用一个 register_shutdown_function 而不是仅在方法内部使用它来捕获该方法中的错误。对于错误捕获,请使用 try、catch、finally 块

请参阅: http: //php.net/manual/en/language.exceptions.php

于 2012-12-04T12:40:58.180 回答