note 110133 added to class.errorexception

From: Date: Sun, 23 Sep 2012 02:33:11 +0000
Subject: note 110133 added to class.errorexception
Groups: php.notes 
Request: Send a blank email to php-notes+get-190966@lists.php.net to get a copy of this message
The example in the manual page as at Sep 22, 2012 was as follows: <?php function exception_error_handler($errno, $errstr, $errfile, $errline ) { throw new ErrorException($errstr, $errno, 0, $errfile, $errline); } set_error_handler("exception_error_handler"); /* Trigger exception */ strpos(); ?> Please note that property $severity of class ErrorException is set to a constant zero for all kinds of errors in the above example. I think it was a bug and tried to file a bug report, but it was closed as not a bug, so I could not say the above is wrong. Let me show an example that uses $severity not as a constant: <?php set_error_handler(function ($errno, $errstr, $errfile, $errline) { throw new ErrorException($errstr, 0, $errno, $errfile, $errline); }); class MyClass { public function methodA() { echo("methodA:\n"); strpos(); } public function methodB() { echo("methodB:\n"); trigger_error("warning message form methodB", E_WARNING); } public function methodC() { echo("methodC:\n"); throw new ErrorException(); } public function methodD() { echo("methodD:\n"); throw new ErrorException('warning message from methodD', 0, E_WARNING); } public function run($i) { if ($i === 0) { $this->methodA(); } else if ($i === 1) { $this->methodB(); } else if ($i === 2) { $this->methodC(); } else { $this->methodD(); } } public function test() { for ($i = 0; $i < 4; ++$i) { try { $this->run($i); } catch (ErrorException $e) { if ($e->getSeverity() === E_ERROR) { echo("E_ERROR triggered.\n"); } else if ($e->getSeverity() === E_WARNING) { echo("E_WARNING triggered.\n"); } } } } } $myClass = new MyClass(); $myClass->test(); ?> Please note that methodC() uses (constructor of) class ErrorException with default parameters. I believe it is the original intention to make $severity having default value of 1, which is exactly equal to E_ERROR. Using property $code or Exception::getCode() to compare with E_* values could not do the same thing (as in methodC()), as $code has a default value of 0, and class Exception has it too, users may use $code for some other purposes. Please also note that in the example of the manual page of Exception::getCode(), the method returns 30, while there is no matching E_* values. I believe it is very clear why most note contributors would use methods similar to the latter example but not the same as the former one. ---- Server IP: 180.92.182.149 Probable Submitter: 124.244.171.10 ---- Manual Page -- http://www.php.net/manual/en/class.errorexception.php Edit -- https://master.php.net/note/edit/110133 Del: integrated -- https://master.php.net/note/delete/110133/integrated Del: useless -- https://master.php.net/note/delete/110133/useless Del: bad code -- https://master.php.net/note/delete/110133/bad+code Del: spam -- https://master.php.net/note/delete/110133/spam Del: non-english -- https://master.php.net/note/delete/110133/non-english Del: in docs -- https://master.php.net/note/delete/110133/in+docs Del: other reasons-- https://master.php.net/note/delete/110133 Reject -- https://master.php.net/note/reject/110133 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#190966) next »