可以关闭库代码的 E_STRICT 但不能关闭我的代码吗?

发布于 2024-08-22 10:05:57 字数 285 浏览 3 评论 0 原文

是否可以更改我的 PHP 应用程序通过 includerequire_once 包含的文件的错误报告级别(关闭 E_STRICT)?

我希望能够看到代码中出现的严格通知,但我使用的是 PEAR MDB2,当我打开 E_STRICT 时,我会从该代码中收到几页警告。

我知道可以使用 .htaccess 文件在每个目录上更改 error_reporting ,但我认为这不适用于包含的文件。我尝试将其放入 pear 文件夹中,但没有任何作用。

Is it possible to change the error reporting level (turn off E_STRICT) for files that my PHP application includes with include or require_once?

I'd like to be able to see strict notices that occur in my code, but I'm using PEAR MDB2, and I get pages of warnings from that code when I turn on E_STRICT.

I know that it's possible to change error_reporting on a per-directory basis with an .htaccess file but I don't think that works with included files. I tried putting it in the pear folder but it did nothing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷离° 2024-08-29 10:05:57

您可以定义自定义错误处理程序,并使用$ errfile 参数来确定错误来自何处。如果路径与包含的库的路径匹配,请抑制错误。否则,将其传递给 PHP 的错误报告。

据我所知,这应该捕获由库引起的所有警告和通知。

因为不需要回溯,所以对于许多触发消息来说它甚至可能足够快。

这未经测试,但根据手册中的示例应该可以工作:

<?php
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
          
    $library_path = "/path/to/library";
    if (substr($errfile,0,strlen($library_path))==$library_path)
    /* Don't execute PHP internal error handler */
     return true;
    else
    /* execute PHP internal error handler */
     return false;
}

You could define a custom error handler, and use the $errfile argument to determine where the error came from. If the path matches the path your included library, suppress the error. Otherwise, pass it on to PHP's error reporting.

As far as I can see, this should catch any and all warnings and notices caused by the library.

Because no backtrace is necessary, it's probably even fast enough for a lot of triggered messages.

this is untested but should work, based on the example in the manual:

<?php
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
          
    $library_path = "/path/to/library";
    if (substr($errfile,0,strlen($library_path))==$library_path)
    /* Don't execute PHP internal error handler */
     return true;
    else
    /* execute PHP internal error handler */
     return false;
}
小ぇ时光︴ 2024-08-29 10:05:57

您可以使用 error_reporting 设置>ini_set()。这是一个例子:

// your running code using the default error reporting setting

// set the error reporting level for your library calls
ini_set('error_reporting', E_NOTICE);

// make some library calls

// reset the error reporting level back to strict
ini_set('error_reporting', E_ALL & E_STRICT);

// more of your code

You can change the error_reporting setting dynamically at runtime using ini_set(). Here is an example:

// your running code using the default error reporting setting

// set the error reporting level for your library calls
ini_set('error_reporting', E_NOTICE);

// make some library calls

// reset the error reporting level back to strict
ini_set('error_reporting', E_ALL & E_STRICT);

// more of your code
遥远的她 2024-08-29 10:05:57

不,不可能。但这

ini_set('error_reporting', E_NOTICE);

会影响您的所有函数/方法调用,即使它们是在其他/库文件中定义的。

Nope, not possible. There's

ini_set('error_reporting', E_NOTICE);

But this will affect all your function/method calls even if they're defined in other/library files.

执笏见 2024-08-29 10:05:57

作为一个非常肮脏的黑客,您可以扩展所有类并依赖神奇的 __call 方法。这不是我的想法,所以不要因为打字错误/脑残而射击我:

class MyDb {
    protected $pearDb; // Instantiate this in your constructor.
    public function __call() {
        $oldReporting = error_reporting(~E_STRICT);
        $result = call_user_func_array(array($this->pearDb, __FUNCTION__), func_get_args());
        error_reporting($oldReporting);
        return $result;
    }
}

如果您希望我更详细地解决它,请告诉我。

As a very dirty hack, you could extend all of the classes and rely on the magic __call method. This is off the top of my head, so don't shoot me for typos/brainfarts:

class MyDb {
    protected $pearDb; // Instantiate this in your constructor.
    public function __call() {
        $oldReporting = error_reporting(~E_STRICT);
        $result = call_user_func_array(array($this->pearDb, __FUNCTION__), func_get_args());
        error_reporting($oldReporting);
        return $result;
    }
}

Let me know if you want me to work it out in more detail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文