Thanks. I must've misread that back then.Originally Posted by sleepeasy
| SitePoint Sponsor |




Thanks. I must've misread that back then.Originally Posted by sleepeasy




This is actually very useful piece of information. I have modified my code accordingly:Originally Posted by sleepeasy
Now the next step would be using some kind of bitwise logic to find out if severity is contained within error_reporting, to get rid of the $throwable array whatsoever, but I don't have the time right now to look for it. Anyone?PHP Code:function exceptions_error_handler($severity, $message, $filename, $lineno)
{
$throwable = array(E_ERROR,
E_WARNING,
E_PARSE,
//E_NOTICE,
E_CORE_ERROR,
E_CORE_WARNING,
E_COMPILE_ERROR,
E_COMPILE_WARNING,
E_USER_ERROR,
E_USER_WARNING,
E_USER_NOTICE,
E_ALL,
//E_STRICT,
);
//var_dump($severity);
if (ini_get('error_reporting') && in_array($severity, $throwable)) {
throw new ErrorException($message, 0, $severity, $filename, $lineno);
}
}
As I posted previously..Originally Posted by BerislavLopac
Which allows using error_reporting() to set which E_* severities cause an exception, and if @ is used and error_reporting() is zero, so the condition still fails.PHP Code:function errorToExceptionHandler($severity, $message, $file, $line)
{
if (error_reporting() & $severity)
throw new ErrorException($message, 0, $severity, $file, $line);
}




Actually, I just go to that myself later last night, just didn't get around to post it.
Missed your previous post with it, though...
Hmm, there still seems some issues with throwing exceptions on E_* errors. The exception getTrace() returns the errorToExceptionHandler function name. And no proper way to correct this. Other than if ($exception instanceof ErrorException) { .. fixup .. }.





Yeah, it would be a nice homework for zend devs to write 1000 times: "I will never use privates/finals in core classes".![]()
Yes, opened a bug for it anyways.Originally Posted by stereofrog
Hah.
Bug got marked as bogus, its apparently expected behaviour!?
http://bugs.php.net/bug.php?id=37224




What did you expect?Originally Posted by Ren
It is expected, as in "it's said so in the manual". A classic example of the "it's not a bug, it's a feature" syndrome that has apparently infested the PHP team recently.





The guy doesn't seem to have bothered to read your post fully... ;(Originally Posted by Ren
The actual problem is that ErrorException modifies trace in an incorrect manner, so that 'args' member is shifted one frame up. This is the specific ErrorExpression problem, shift doesn't occur with ordinary Exception. Illustration:
PHP Code:function errorneous($a, $b, $c) {
try { 1/0; }
catch(Exception $e) {
$GLOBALS['from_excp'] = $e->getTrace();
}
}
//-----------------------------------------------
// test with basic Exception
function _err_to_basic_ex($code, $msg, $file, $line) {
$GLOBALS['from_backtrace'] = debug_backtrace();
throw new Exception();
}
echo "Exception...\n";
set_error_handler('_err_to_basic_ex');
errorneous('args', 'of', 'errorneous');
# with Exception both traces are equal
var_dump($from_backtrace == $from_excp);
//-----------------------------------------------
// test with ErrorException
function _err_to_error_ex($code, $msg, $file, $line) {
$GLOBALS['from_backtrace'] = debug_backtrace();
throw new ErrorException($msg, 0, $code, $file, $line);
}
echo "ErrorException...\n";
set_error_handler('_err_to_error_ex');
errorneous('args', 'of', 'errorneous');
# with ErrorException both traces are NOT equal
# because args are shifted
var_dump($from_backtrace == $from_excp);
Hmm, just had an idea
How about having an exception for asserts() too...
Not sure I like it yet.. thou I think thats in part due to the nature of PHPs assert() being eval() like.PHP Code:<?php
class AssertException extends ErrorException
{
function __construct($script, $line, $message)
{
parent::__construct('Assertion Exception', 0, E_USER_WARNING, $script, $line);
}
}
function assertToExceptionHandler($script, $line, $message )
{
throw new AssertException($script, $line, $message);
}
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 0);
assert_options(ASSERT_CALLBACK, 'assertToExceptionHandler');
function test($a)
{
assert('$a > 0 && $a < 10');
}
test(11);
Bookmarks