views:

278

answers:

4

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.

+3  A: 

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
Asaph
he precisely asked something completely different.
Adam Kiss
This addresses his issue. Allow me to expand my answer and explain why. He can set the error reporting level, make a library call, and then set it back.
Asaph
@Adam Kiss : no, he's not... he said he wanted something different than using .htaccess, this _is_ something different than using .htacess. Asaph could be clearer tho, and show that you can include the library call between the two calls to `ini_set()`.
gnud
@Adam Kiss and gnud: I've expanded my answer. Please review.
Asaph
can't you aswell include library between `error_reporting()`? yes you can. But that doesn't solve error reporting if you later use functions from that libraries, does it?
Adam Kiss
I would like to delete -1 (I apologize, I think it was a bit too harsh) but I can't - vote too old to delete...
Adam Kiss
@Adam Kiss: I appreciate that. I've made a minor edit that should enable you to remove your downvote. Thanks :)
Asaph
@Asaph - I still think it's a bit off than what question was, but I don't really need to be harsh...
Adam Kiss
A: 

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.

partoa
A: 

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.

janmoesen
+3  A: 

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;
}
Pekka
janmoesen
@janmoesen I think the OP's issue is that the library (which he can't influence) causes notices and warnings, and he wants to shut them up altogether. If this is not the case, checking for the error level is indeed a good idea.
Pekka