note 110133 modified in class.errorexception by googleguy

From: Date: Sun, 23 Sep 2012 02:55:51 +0000
Subject: note 110133 modified in class.errorexception by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190967@lists.php.net to get a copy of this message
The example in the manual page as at Sep 22, 2012 was as follows:&#13;&#10;&#60;?php&#13;&#10;function exception_error_handler($errno, $errstr, $errfile, $errline ) {&#13;&#10; throw new ErrorException($errstr, $errno, 0, $errfile, $errline);&#13;&#10;}&#13;&#10;set_error_handler(&#34;exception_error_handler&#34;);&#13;&#10;&#13;&#10;/* Trigger exception */&#13;&#10;strpos();&#13;&#10;?&#62;&#13;&#10;&#13;&#10;Please note that property $severity of class ErrorException is set to a &#13;&#10;constant zero for all kinds of errors in the above example.&#13;&#10;&#13;&#10;I think it was a bug and tried to file a bug report, but it was &#13;&#10;closed as not a bug, so I could not say the above is wrong.&#13;&#10;&#13;&#10;Let me show an example that uses $severity not as a constant:&#13;&#10;&#60;?php&#13;&#10; set_error_handler(function ($errno, $errstr, $errfile, $errline) {&#13;&#10; throw new ErrorException($errstr, 0, $errno, $errfile, $errline);&#13;&#10; });&#13;&#10;&#13;&#10; class MyClass {&#13;&#10; public function methodA() {&#13;&#10; echo(&#34;methodA:\\n&#34;);&#13;&#10; strpos();&#13;&#10; }&#13;&#10;&#13;&#10; public function methodB() {&#13;&#10; echo(&#34;methodB:\\n&#34;);&#13;&#10; trigger_error(&#34;warning message form methodB&#34;, E_WARNING);&#13;&#10; }&#13;&#10;&#13;&#10; public function methodC() {&#13;&#10; echo(&#34;methodC:\\n&#34;);&#13;&#10; throw new ErrorException();&#13;&#10; }&#13;&#10;&#13;&#10; public function methodD() {&#13;&#10; echo(&#34;methodD:\\n&#34;);&#13;&#10; throw new ErrorException(&#39;warning message from methodD&#39;, 0, &#13;&#10;E_WARNING);&#13;&#10; }&#13;&#10;&#13;&#10; public function run($i) {&#13;&#10; if ($i === 0) {&#13;&#10; $this-&#62;methodA();&#13;&#10; } else if ($i === 1) {&#13;&#10; $this-&#62;methodB();&#13;&#10; } else if ($i === 2) {&#13;&#10; $this-&#62;methodC();&#13;&#10; } else {&#13;&#10; $this-&#62;methodD();&#13;&#10; }&#13;&#10; }&#13;&#10;&#13;&#10; public function test() {&#13;&#10; for ($i = 0; $i &#60; 4; ++$i) {&#13;&#10; try {&#13;&#10; $this-&#62;run($i);&#13;&#10; } catch (ErrorException $e) {&#13;&#10; if ($e-&#62;getSeverity() === E_ERROR) {&#13;&#10; echo(&#34;E_ERROR triggered.\\n&#34;);&#13;&#10; } else if ($e-&#62;getSeverity() === E_WARNING) {&#13;&#10; echo(&#34;E_WARNING triggered.\\n&#34;);&#13;&#10; }&#13;&#10; }&#13;&#10; }&#13;&#10; }&#13;&#10; }&#13;&#10;&#13;&#10; $myClass = new MyClass();&#13;&#10; $myClass-&#62;test();&#13;&#10;?&#62;&#13;&#10;&#13;&#10;Please note that methodC() uses (constructor of) class &#13;&#10;ErrorException with default parameters.&#13;&#10;&#13;&#10;I believe it is the original intention to make $severity having default &#13;&#10;value of 1, which is exactly equal to E_ERROR.&#13;&#10;&#13;&#10;Using property $code or Exception::getCode() to compare with E_* values &#13;&#10;could not do the same thing (as in methodC()), as $code has a default &#13;&#10;value of 0, and class Exception has it too, users may use $code for some &#13;&#10;other purposes.&#13;&#10;&#13;&#10;I believe it is very clear why most note contributors would use methods &#13;&#10;similar to the latter example but not the same as the former one. --was-- 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. http://php.net/manual/en/class.errorexception.php

« previous php.notes (#190967) next »