|
Posted by Daniel Smedegaard Buus on November 7, 2008, 3:41 am
Please log in for more thread options
Hey all :)
I was wondering about the $error_types (I particularly notice the 's' suffix
when reading the manual) parameter for 'set_error_handler()':
Can be used to mask the triggering of the error_handler function just like
the error_reporting ini setting controls which errors are shown. Without
this mask set the error_handler will be called for every error regardless
to the setting of the error_reporting setting.
And, in my php.ini I have,
error_reporting = E_ALL & ~E_NOTICE
Which - I think - means something like "E_ALL and less severe AND NOT
E_NOTICE in particular".
This may be read wrong, but in essence, what I assume is possible is to
create a mask that very particularly lets you define which errors to handle
with your custom error handler, and which not to. But I cannot find any
documentation telling me how this works.
I would like, rather than having to do something like -
if (DEVELOPMENT_MODE && in_array($error_level, array(E_WARNING,
E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE, E_RECOVERABLE_ERROR,
E_DEPRECATED, E_USER_DEPRECATED)) log_to_screen($message);
- on every error/notice/whatever comes by my custom error handler, to be
able to specify a mask that essentially does the same only in my
set_error_handler call.
This may seem superfluous, but what I'm creating right now are core services
that should service upwards of hundreds of frontends simultaneously, so
whatever small performance gain I can achieve here and there may lead to a
tremendous effect overall.
Can anyone help me with understanding this masking business? Or I have I
misunderstood the concept entirely? :)
Thanks in advance,
Daniel :)
|
|
Posted by Erwin Moller on November 7, 2008, 8:05 am
Please log in for more thread options
Daniel Smedegaard Buus schreef:
show/hide quoted text
> Hey all :)
>
> I was wondering about the $error_types (I particularly notice the 's' suffix
> when reading the manual) parameter for 'set_error_handler()':
>
> Can be used to mask the triggering of the error_handler function just like
> the error_reporting ini setting controls which errors are shown. Without
> this mask set the error_handler will be called for every error regardless
> to the setting of the error_reporting setting.
>
> And, in my php.ini I have,
> error_reporting = E_ALL & ~E_NOTICE
>
> Which - I think - means something like "E_ALL and less severe AND NOT
> E_NOTICE in particular".
>
> This may be read wrong, but in essence, what I assume is possible is to
> create a mask that very particularly lets you define which errors to handle
> with your custom error handler, and which not to. But I cannot find any
> documentation telling me how this works.
>
> I would like, rather than having to do something like -
>
> if (DEVELOPMENT_MODE && in_array($error_level, array(E_WARNING,
> E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE, E_RECOVERABLE_ERROR,
> E_DEPRECATED, E_USER_DEPRECATED)) log_to_screen($message);
>
> - on every error/notice/whatever comes by my custom error handler, to be
> able to specify a mask that essentially does the same only in my
> set_error_handler call.
>
> This may seem superfluous, but what I'm creating right now are core services
> that should service upwards of hundreds of frontends simultaneously, so
> whatever small performance gain I can achieve here and there may lead to a
> tremendous effect overall.
>
> Can anyone help me with understanding this masking business? Or I have I
> misunderstood the concept entirely? :)
>
> Thanks in advance,
> Daniel :)
Hi Daniel,
I think you need to reread the manual:
http://nl3.php.net/manual/en/function.set-error-handler.php
[quote]
It is important to remember that the standard PHP error handler is
completely bypassed. error_reporting() settings will have no effect and
your error handler will be called regardless - however you are still
able to read the current value of error_reporting and act appropriately.
Of particular note is that this value will be 0 if the statement that
caused the error was prepended by the @ error-control operator.
[/quote]
So your custom errorhandler is called ALWAYS regardless the settings you
put into error_reporting() (or via php.ini).
Your errorhandler must deal with this error then.
The errorhandler is called with a bunch of parameters, the first being
the errornumber as listed here:
http://nl3.php.net/manual/en/function.error-reporting.php
So I am unsure why you care about the 'masking business', since it is of
no concern to you (unless I completely misinterpret your posting).
The passed errornumber can only be 1 of the errors listed (You cannot
have 2 errors at the same time).
Also, if you write your own handler, be sure you check for errorsupression.
Here is the first peice of code of an errorhandler I use often:
function errorHandler($number, $string, $file, $line, $context) {
// If the user uses errorsurperrsion with @, we need to forgive this error!
if (error_reporting() == 0){
// The error was surpressed, return without futher interference.
return;
}
// rest of the errorhandler
}
Good luck,
Regards,
Erwin Moller
--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
|
|
Posted by Daniel Smedegaard Buus on November 7, 2008, 11:37 am
Please log in for more thread options Erwin Moller wrote:
show/hide quoted text
> Daniel Smedegaard Buus schreef:
>> Hey all :)
>>
>> I was wondering about the $error_types (I particularly notice the 's'
>> suffix when reading the manual) parameter for 'set_error_handler()':
>>
>> Can be used to mask the triggering of the error_handler function just
>> like the error_reporting ini setting controls which errors are shown.
>> Without
>> this mask set the error_handler will be called for every error
>> regardless to the setting of the error_reporting setting.
>>
>> And, in my php.ini I have,
>> error_reporting = E_ALL & ~E_NOTICE
>>
>> Which - I think - means something like "E_ALL and less severe AND NOT
>> E_NOTICE in particular".
>>
>> This may be read wrong, but in essence, what I assume is possible is to
>> create a mask that very particularly lets you define which errors to
>> handle with your custom error handler, and which not to. But I cannot
>> find any documentation telling me how this works.
>>
>> I would like, rather than having to do something like -
>>
>> if (DEVELOPMENT_MODE && in_array($error_level, array(E_WARNING,
>> E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE, E_RECOVERABLE_ERROR,
>> E_DEPRECATED, E_USER_DEPRECATED)) log_to_screen($message);
>>
>> - on every error/notice/whatever comes by my custom error handler, to be
>> able to specify a mask that essentially does the same only in my
>> set_error_handler call.
>>
>> This may seem superfluous, but what I'm creating right now are core
>> services that should service upwards of hundreds of frontends
>> simultaneously, so whatever small performance gain I can achieve here and
>> there may lead to a tremendous effect overall.
>>
>> Can anyone help me with understanding this masking business? Or I have I
>> misunderstood the concept entirely? :)
>>
>> Thanks in advance,
>> Daniel :)
>
> Hi Daniel,
>
> I think you need to reread the manual:
> http://nl3.php.net/manual/en/function.set-error-handler.php
>
> [quote]
> It is important to remember that the standard PHP error handler is
> completely bypassed. error_reporting() settings will have no effect and
> your error handler will be called regardless - however you are still
> able to read the current value of error_reporting and act appropriately.
> Of particular note is that this value will be 0 if the statement that
> caused the error was prepended by the @ error-control operator.
> [/quote]
>
Actually, I did read the manual ;) and this part too, but I'm not using
error_reporting(), I'm referring to the last parameter in
set_error_handler(callback $error_handler [, int $error_types]), which is,
and I quote (http://dk2.php.net/set_error_handler),
[quote]
error_types
Can be used to mask the triggering of the error_handler function just like
the error_reporting ini setting controls which errors are shown. Without
this mask set the error_handler will be called for every error regardless
to the setting of the error_reporting setting.
[/quote]
Which is, in fact, the same quote as in my previous post ;) So, this has the
same effect as error_reporting() would have on the built-in error handler,
only on the custom one.
show/hide quoted text
> So your custom errorhandler is called ALWAYS regardless the settings you
> put into error_reporting() (or via php.ini).
>
> Your errorhandler must deal with this error then.
> The errorhandler is called with a bunch of parameters, the first being
> the errornumber as listed here:
> http://nl3.php.net/manual/en/function.error-reporting.php
>
> So I am unsure why you care about the 'masking business', since it is of
> no concern to you (unless I completely misinterpret your posting).
>
> The passed errornumber can only be 1 of the errors listed (You cannot
> have 2 errors at the same time).
>
> Also, if you write your own handler, be sure you check for
> errorsupression.
>
> Here is the first peice of code of an errorhandler I use often:
>
> function errorHandler($number, $string, $file, $line, $context) {
> // If the user uses errorsurperrsion with @, we need to forgive this
> error!
> if (error_reporting() == 0){
> // The error was surpressed, return without futher interference.
> return;
> }
> // rest of the errorhandler
>
I did not know of this. Very useful tip! I have previously turned
error_reporting on and off on-the-fly to achieve the same! Thank you very
much :)
Daniel
show/hide quoted text
> }
>
> Good luck,
>
> Regards,
> Erwin Moller
>
|
|
Posted by Charles Calvert on November 11, 2008, 5:13 pm
Please log in for more thread options On Tue, 11 Nov 2008 08:45:36 +0100, Daniel Smedegaard Buus
[snip explanation of bit fields]
show/hide quoted text
>May I ask what the AND operator does?
Assuming that you mean the "&" operator, it keeps only the bits that
are set in both variables. E.g. 2 & 3 = 2
00000010 // 2 base 2
00000011 // 3 base 2
--------
00000010 // keeps only the bit in the two's place
// because it's the only one set in both
show/hide quoted text
>And the ~ in front of a constant, is that only for the php.ini
>setting, or is that also valid in php, and if so, what does it mean?
It's the complement operator, which inverts the bits. E.g. if 2 is
00000010 then it's complement is 11111101. It's useful for removing a
bit from a bitfield. Saying E_ALL & ~ E_NOTICE is a way of saying
"everything except for notices".
--
Charles Calvert | Web-site Design/Development
Celtic Wolf, Inc. | Software Design/Development
http://www.celticwolf.com/ | Data Conversion
(703) 580-0210 | Project Management
|
|
Posted by Daniel Smedegaard Buus on November 12, 2008, 3:10 pm
Please log in for more thread options Charles Calvert wrote:
show/hide quoted text
> On Tue, 11 Nov 2008 08:45:36 +0100, Daniel Smedegaard Buus
>
> [snip explanation of bit fields]
>
>>May I ask what the AND operator does?
>
> Assuming that you mean the "&" operator, it keeps only the bits that
> are set in both variables. E.g. 2 & 3 = 2
>
> 00000010 // 2 base 2
> 00000011 // 3 base 2
> --------
> 00000010 // keeps only the bit in the two's place
> // because it's the only one set in both
>
>
>>And the ~ in front of a constant, is that only for the php.ini
>>setting, or is that also valid in php, and if so, what does it mean?
>
> It's the complement operator, which inverts the bits. E.g. if 2 is
> 00000010 then it's complement is 11111101. It's useful for removing a
> bit from a bitfield. Saying E_ALL & ~ E_NOTICE is a way of saying
> "everything except for notices".
Yes, thank you both :) I did the obligatory Googling, and ended up with an
extremely explanatory C tutorial on bitwise operations, and luckily, this
applies 1:1 on PHP :)
Funny, from the outside it seems like a bad thing to be happy that you just
made handling of errors in your code faster ;) He he. Well, we gotta be
realistic beings, right? ;)
Another funny thing is that reading this, everything just fell into place.
I've been taught this as part of my education, specifically when learning
assembly language, but for some reason it just doesn't stick with me... I
really like languages like Ruby :D
Either way, It's great to revisit basic stuff like this, because it really
adds a lot of understanding to knowing what you're actually doing when you
declare a class or use something basic like control structures. It's really
healthy to learn this, you know :)
Cheers to you both,
Daniel :)
|
| Similar Threads | Posted | | error handling | January 14, 2005, 4:02 pm |
| MAX_FILE_SIZE & error handling? | September 1, 2004, 1:05 pm |
| Syntax error handling | March 9, 2005, 5:37 am |
| Custom error handling | August 9, 2005, 9:43 am |
| XSL error handling in PHP5 | April 18, 2006, 12:37 pm |
| session_set_save_handler and error handling | July 13, 2007, 4:29 pm |
| Re: php error handling with apache | October 31, 2008, 3:33 pm |
| query error handling | December 18, 2009, 9:44 am |
| mysql class, error handling | July 12, 2006, 6:37 am |
| error handling issue - try / catch with PHP 5 | September 12, 2008, 11:14 am |
|
>
> I was wondering about the $error_types (I particularly notice the 's' suffix
> when reading the manual) parameter for 'set_error_handler()':
>
> Can be used to mask the triggering of the error_handler function just like
> the error_reporting ini setting controls which errors are shown. Without
> this mask set the error_handler will be called for every error regardless
> to the setting of the error_reporting setting.
>
> And, in my php.ini I have,
> error_reporting = E_ALL & ~E_NOTICE
>
> Which - I think - means something like "E_ALL and less severe AND NOT
> E_NOTICE in particular".
>
> This may be read wrong, but in essence, what I assume is possible is to
> create a mask that very particularly lets you define which errors to handle
> with your custom error handler, and which not to. But I cannot find any
> documentation telling me how this works.
>
> I would like, rather than having to do something like -
>
> if (DEVELOPMENT_MODE && in_array($error_level, array(E_WARNING,
> E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE, E_RECOVERABLE_ERROR,
> E_DEPRECATED, E_USER_DEPRECATED)) log_to_screen($message);
>
> - on every error/notice/whatever comes by my custom error handler, to be
> able to specify a mask that essentially does the same only in my
> set_error_handler call.
>
> This may seem superfluous, but what I'm creating right now are core services
> that should service upwards of hundreds of frontends simultaneously, so
> whatever small performance gain I can achieve here and there may lead to a
> tremendous effect overall.
>
> Can anyone help me with understanding this masking business? Or I have I
> misunderstood the concept entirely? :)
>
> Thanks in advance,
> Daniel :)