Doc #63125 [Opn]: Wrong Example of Class ErrorException
| From: | googleguy@php.net | Date: | Fri, 21 Sep 2012 10:30:08 +0000 |
| Subject: | Doc #63125 [Opn]: Wrong Example of Class ErrorException | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-8872@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=63125&edit=1
ID: 63125
Updated by: googleguy@php.net
Reported by: xianrenb at gmail dot com
Summary: Wrong Example of Class ErrorException
Status: Open
Type: Documentation Problem
Package: Documentation problem
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
To address your concerns in order:
1. The phrase "error level" throughout the PHP documentation is ubiquitous with E_* error
constants, as in E_ALL, E_ERROR, E_PARSE, E_STRICT, etc... The error handler expects this
value as an integer. So for example an E_ERROR constant has an integer value of 1, whereas
an E_STRICT may have an integer value of 2048 (depending on which version of PHP you're
using). These integer values, however, do not necessarily depict the "severity" of an
error. At least not in any obvious way, in terms of their value.
For example, E_STRICT has a larger integer value than E_USER_ERROR and even E_PARSE. This
doesn't mean E_STRICT is necessarily more severe than E_PARSE. If anything an E_PARSE
error is very severe since it's fatal where as an E_USER_ERROR is not. These error level
constants have values that are meaningful to the PHP error handler, and not necessarily
going to be meaningful to your Exception handler.
2. As for this it's completely up to you what you set $severity or $code to in your
ErrorException class. Here the documentation is just passing the $errorno from the error
handler to the $code property in the ErrorException class in the event you'd like to use
it in your Exception handler. As you pointed out Exception Class does not have a $severity
property, but it does have a $code property that can carry error codes. Here the idea
would be to set $severity to something for what would normally translate to fatal errors
and probably not set it for non-fatal errors like E_NOTICE, E_WARNING, etc..
3. By default all Exceptions are fatal unless caught. It's still up to you to decide
whether or not you'd like your Exception to carry a severity that is a 1:1 translation of
the error code passed from the error handler. This is not a requirement since it doesn't
actually end up affecting the behavior of ErrorException.
4. This doesn't mean there is a bug in the documentation.
Thanks
Previous Comments:
------------------------------------------------------------------------
[2012-09-21 05:50:27] xianrenb at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/class.errorexception
---
I believe the example in the manual page as at Sep 21, 2012 is wrong:
1. According to the manual page of set_error_handler(), $errno contains
the level of the error raised, as an integer. This is the same meaning
as $severity of class ErrorException.
2. Class ErrorException has property $severity, but class Exception does
not have the same property. It only makes sense if $severity is set to
something useful (non-zero), and related to error.
3. $severity has a default value of 1, which is the same value as
E_ERROR, while $code has a default value of 0.
4. Most notes contributed by other PHP users, if not all, use
ErrorException in the same way as in the expected result suggested.
Expected result:
----------------
Example documented as:
<?php
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");
/* Trigger exception */
strpos();
?>
Actual result:
--------------
Example documented as:
<?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();
?>
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=63125&edit=1