heiglandreas, (edited )
@heiglandreas@phpc.social avatar

TIL that

$a = set_error_handler('my_error_handler');
set_error_handler($a);

is something different than

set_error_handler('my_error_handler');
restore_error_handler();

🤯

Edit: Fixed typos

bhhaskin,
@bhhaskin@bitsofsimplicity.com avatar

@heiglandreas well duh! Ones got a $a in it!

heiglandreas,
@heiglandreas@phpc.social avatar

@bhhaskin And the other one got a re ...

velkuns,
@velkuns@phpc.social avatar

@heiglandreas @bhhaskin the first part is back to previous handler (that may not be the initial one, because it had may be redefined preciously).

The second one just reset the handler to the original default state.

Effectively, it makes sense when we take time to think about it 👍

heiglandreas,
@heiglandreas@phpc.social avatar

@velkuns @bhhaskin

The first one sets the previous handler again, whereas the second one resets to the previous handler.

Both do not necessarily set to the original default state! (at least according to the docs)

Exakat,
@Exakat@phpc.social avatar

@heiglandreas @bhhaskin One is missing a 'L' 😇

If the built-in error handler is used null is returned. So, you can't reset the handler by giving it the returned value of the first call. Quite surprising, indeed.

heiglandreas,
@heiglandreas@phpc.social avatar

@Exakat You can set the error handler by calling set_error_handler(null). It will set the error handler to the default state - according to the docs at least...

@bhhaskin

Exakat,
@Exakat@phpc.social avatar

@heiglandreas @bhhaskin so, undocumented behavior. 😭

gmazzap,
@gmazzap@phpc.social avatar

@Exakat set_error_handler first param is nullable. So if the first call to it returns null (when default handler is in use) in the first snippet you'd call set_error_handler the second time with a null argument, which ends up set the default handler (according to docs).

So @heiglandreas what's the difference?

@bhhaskin

heiglandreas,
@heiglandreas@phpc.social avatar

@gmazzap

The difference seems to be that they kind of work on a stack. set_error_handler adds to the stack and restore_Error_handler removes from the stack. But when one calls restore_ twice it seems to use an unintended error handler. At least that was my explanation based on the observation that suddenly an error handler was used that I thought I had "unset"...

Need to check the sources though to verify that

/cc @Exakat @bhhaskin

heiglandreas,
@heiglandreas@phpc.social avatar

@gmazzap @Exakat @bhhaskin

Checked the sources and indeed:

set_error_handler puts the error handler onto the top of a stack and restore_error_handler removes the topmost error handler from the stack.

So calling

set_error_handler('smth');
$a = set_ERror_handler('handler');
set_error_handler($a);

will create an error handler stack

smth
handler
smth

and

set_error_handler('something');
set_error_handler('handler');
restore_Error_handler();

will create

smth

https://github.com/php/php-src/blob/14bdb01f8cf91024fdcfc0dff7bc65c93598d28e/Zend/zend_builtin_functions.c#L1156-L1213

gmazzap,
@gmazzap@phpc.social avatar

@heiglandreas so the end result after both snippets is that "smth" will be the current error handler.

But the "history" is different, and so calling restore_error_handler again (without a previous call to set_error_handler) will have a different result. Which means the general rule to avoid unexpected results is don't call restore_error_handlerif you haven't calledset_error_handler` first :)
@Exakat @bhhaskin

heiglandreas,
@heiglandreas@phpc.social avatar

@gmazzap @Exakat @bhhaskin

Yes! But also, to restore the previous error-handler! Do not reset it onto the stack but instead use "restore_error_handler"!

I might have to extend the docs to add those findings.

Most people think setting an error handler is a variable assignment and setting another handler will overwrite the current value. Whereas "resetting" it will reset it to it's original state (which most people do not want to do).

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • DreamBathrooms
  • ngwrru68w68
  • mdbf
  • magazineikmin
  • InstantRegret
  • rosin
  • Youngstown
  • slotface
  • ethstaker
  • cubers
  • kavyap
  • GTA5RPClips
  • tester
  • khanakhh
  • JUstTest
  • tacticalgear
  • everett
  • Durango
  • thenastyranch
  • cisconetworking
  • anitta
  • Leos
  • normalnudes
  • osvaldo12
  • modclub
  • megavids
  • provamag3
  • lostlight
  • All magazines