【问题标题】:set_error_handler() receives invalid callback? [duplicate]set_error_handler() 收到无效回调? [复制]
【发布时间】:2016-05-05 04:06:37
【问题描述】:

我收到此错误:

set_error_handler() 期望参数 (userErrorAdvice) 是一个有效的回调


trait userErrorAdviceTrait
{
    public function userErrorAdvice()
    {
        $error = error_get_last();
        $_SESSION['errorStore'] = $error;
        $errorMessage = "No file exists for the PageController";
        if (strstr($error['message'], "No file exists for the PageController class"))
        {
            header('Location: http://192.171.127.39/~louis/errorAdvicePage.php?errorType=NoPageControllerError');
            exit;
        }
    }
    public function setUserErrorAdvice()
    {
        set_error_handler("userErrorAdvice");
    }
}

use userErrorAdviceTrait;
public static function makePageController($pageName) 
{
    //self::shutDownFunction();
    //self::shutdown_function();
    self::setUserErrorAdvice();

    //Rest of code .... 

这迫使我写更多细节,但我无话可说。

【问题讨论】:

  • 对此不是 100% 确定,但我猜这是范围问题。 set_error_handler 不仅适用于特定类,还适用于整个脚本,因此它正在寻找一个名为 userErrorAdvice() 的全局函数。因为它没有发现它会引发该错误。
  • http://192.171.127./~louis[etc] 看起来不是一个有效的 URL。
  • nvm 的 url,这只是我在 stackoveflow 上输入的错误。会调整的。
  • @Flaco_Taco 关于您上次的编辑,不,这不是“强迫”您添加任何细节。如果这个问题感觉很空洞,那是因为它是。不要加绒毛来加绒毛,这也是我一开始去掉它的原因。

标签: php error-handling callback


【解决方案1】:

我为此做了一个简单的工作测试。这是我要查看的代码

<?php

trait userErrorAdviceTrait
{
    public function function userErrorAdvice($errno, $errstr)
    {
        echo 'error no '.$errno.' and message '.$errstr.PHP_EOL;
    }

    public function setUserErrorAdvice()
    {
        set_error_handler("userErrorAdvice");
    }
}

class TestErrorFunction
{
    use userErrorAdviceTrait;

    public static function makePageController($pageName)
    {
        self::setUserErrorAdvice();
    }
}

TestErrorFunction::makePageController('page');

我在这里看到了一些问题。让我解释并修复它们。

  1. 您将非静态函数用作静态函数。当应该执行方法(函数)userErrorAdvice() 时,它应该“具有”和对象。但是没有为函数创建对象。

  2. set_error_handler() 函数的参数应该是 PHP callable。在我们的例子中,没有全局函数 userErrorAdvice()

有几个可能的选项可以解决这些问题。例如

<?php

trait userErrorAdviceTrait
{
    public static function userErrorAdvice($errno, $errstr )
    {
        echo 'error no '.$errno.' and message '.$errstr.PHP_EOL;
    }

    public static function setUserErrorAdvice()
    {
        set_error_handler([userErrorAdviceTrait::class, "userErrorAdvice"]);
        // the other option here
        // set_error_handler('userErrorAdviceTrait::userErrorAdvice');
    }
}

class TestErrorFunction
{
    use userErrorAdviceTrait;

    public static function makePageController($pageName)
    {
        self::setUserErrorAdvice();
        include 'no existing file';
    }
}

TestErrorFunction::makePageController('page');

此代码产生以下输出。

$ php code.php 
error no 2 and message include(no existing file): failed to open stream: No such file or directory
error no 2 and message include(): Failed opening 'no existing file' for inclusion (include_path='.:/usr/share/php')

再来一次我做了什么

  1. 固定的可调用参数。
  2. 将方法定义为静态,因为它们在静态上下文中使用。

【讨论】:

    猜你喜欢
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 2018-11-03
    相关资源
    最近更新 更多