开发者

Possible to turn off E_STRICT for library code but not my code?

开发者 https://www.devze.com 2022-12-21 04:14 出处:网络
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?

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 t开发者_JS百科o 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.


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;
}


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消