note 110133 modified in class.errorexception by googleguy
| From: | googleguy@php.net | 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: <?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. 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.
--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