The right way to handle unavoidable alerts

There are a lot of good old procedural functions, which may cause the prevention of error conditions that may occur during regular work, eg fopen(), mail(), oci_connect()...

  • The most obvious workaround is considered expensive and has the side effect of hiding everything:

    echo @file_get_contents(oops_forgot_dollar);
    
  • A custom error handler seems redundant:

    private function warningHandler($errno, $errstr, $errfile, $errline/*, array $errcontext*/){
        if(error_reporting()===0){
            return false;
        }else{
            throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
        }
    }
    
    // ....
    
    try{
        set_error_handler('CSVLoader::warningHandler', E_WARNING);
        $this->fp = fopen($filename, 'r');
        restore_error_handler();
    
        $this->readData();
    }catch(ErrorException $e){
        restore_error_handler();
        throw new CSVLoaderException("Could not open file: {$e->getMessage()}");
    }
    
  • Avoiding warning skips is annoying even on a properly configured server:

    • During development, they appear in the middle of nowhere and can break things.
    • During production, they flood the error log with useless data.

What is the recommended practice? How do PHP gurus do this?

+4
source share
2

_log -

, , cron, - error_log.....

, .

+1

PHP Packagist :

  • (CodeIgniter, Nette, Swift Mailer, Yii...).
  • set_error_handler(), ErrorException - (Zend, Symfony, Laravel, PHPUnit, Psy...). , (PHPMailer) set/restore.
  • , -, (PHPExcel).

, :

  • , , .
  • (, , , ). , . "" , , , .
  • , , , , .

(*) , @ isset() , @ . , , isset() , , .


, : , - brew - , . : ( ):

<?php

error_reporting(-1);
ini_set('display_errors', false);
ini_set('log_errors', false);


define('ITERATIONS', 10000);


function warning_handler($errno, $errstr, $errfile, $errline/*, array $errcontext*/){
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}


echo sprintf('Running %s iterations on PHP/%s...' . PHP_EOL, number_format(ITERATIONS), phpversion());


$t0 = microtime(true);
for($i=0; $i<ITERATIONS; $i++){
    fopen("FooA $i", 'r');
}
echo sprintf('Raw:                         % 6.3fs' . PHP_EOL, microtime(true) - $t0);


$t0 = microtime(true);
for($i=0; $i<ITERATIONS; $i++){
    @fopen("FooB $i", 'r');
}
echo sprintf('@:                           % 6.3fs' . PHP_EOL, microtime(true) - $t0);


$t0 = microtime(true);
for($i=0; $i<ITERATIONS; $i++){
    set_error_handler('warning_handler', E_WARNING);
    try{
        fopen("FooC $i", 'r');
        restore_error_handler();
    }catch(ErrorException $e){
        restore_error_handler();
    }
}
echo sprintf('Local set_error_handler():   % 6.3fs' . PHP_EOL, microtime(true) - $t0);


$t0 = microtime(true);
set_error_handler('warning_handler', E_WARNING);
for($i=0; $i<ITERATIONS; $i++){
    try{
        fopen("FooD $i", 'r');
    }catch(ErrorException $e){
    }
}
restore_error_handler();
echo sprintf('General set_error_handler(): % 6.3fs' . PHP_EOL, microtime(true) - $t0);

Windows 7 ( PHP x86):

Running 10,000 iterations on PHP/5.3.28...
Raw:                          1.388s
@:                            1.232s
Local set_error_handler():    1.638s
General set_error_handler():  1.497s
Running 10,000 iterations on PHP/5.4.24...
Raw:                          1.170s
@:                            1.185s
Local set_error_handler():    1.357s
General set_error_handler():  1.326s
Running 10,000 iterations on PHP/5.5.22...
Raw:                          1.185s
@:                            1.341s
Local set_error_handler():    4.960s
General set_error_handler():  4.570s
Running 10,000 iterations on PHP/5.6.10...
Raw:                          1.139s
@:                            1.139s
Local set_error_handler():    1.263s
General set_error_handler():  1.232s
Running 10,000 iterations on PHP/7.0.0beta1...
Raw:                          1.232s
@:                            1.279s
Local set_error_handler():    1.669s
General set_error_handler():  1.669s
+1

All Articles