Page 1 of 1

Call function before/after Fatal Error?

Posted: Mon Jan 11, 2010 9:57 am
by infolock
It currently isn't possible to call a function before/after a Fatal Error. This is too bad as it would be extremely useful to call a uninstall or revert function in the case of a fatal error. Any ideas if this feature is really going to be part of PHP 6, or is it all just a bunch of heresay?

Re: Call function before/after Fatal Error?

Posted: Mon Jan 11, 2010 9:59 am
by Charles256

Re: Call function before/after Fatal Error?

Posted: Mon Jan 11, 2010 10:08 am
by infolock
No because not all errors that are fatal can be handled...

from the manal: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT

Also, issues where a function call does not exist, an object doesn't exist, or the like also cannot be handled.

Re: Call function before/after Fatal Error?

Posted: Mon Jan 11, 2010 10:11 am
by Charles256
What fatal errors are you experiencing that even brings this to mind if you don't mind me asking?

Re: Call function before/after Fatal Error?

Posted: Mon Jan 11, 2010 10:16 am
by infolock
Errors like "Fatal error: Call to undefined method blah::insert", or "Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in blahblah.php", etc etc...

Re: Call function before/after Fatal Error?

Posted: Mon Jan 11, 2010 10:21 am
by infolock
FYI, this is happening during an installation process I have that is installing some other developer's code. It executes that devs installation, and should a fatal error, or hell any kind of error ever happen, I want to run a revert operation. But, again, it's currently not supported to this level yet. I have heard whispers that it's coming to PHP 6, but I'm not sure if it's entirely true or not. was just curious...

Re: Call function before/after Fatal Error?

Posted: Mon Jan 11, 2010 10:29 am
by Charles256
Ah. tell him to stop writing crappy code? :-D

Re: Call function before/after Fatal Error?

Posted: Mon Jan 11, 2010 10:48 am
by Benjamin
infolock wrote:It currently isn't possible to call a function before/after a Fatal Error.
Well I must say, I'm quite curious to know how one would plan on executing an error handler before the error occurs...

Re: Call function before/after Fatal Error?

Posted: Mon Jan 11, 2010 12:26 pm
by infolock
astions: i don't exactly proofread my posts before posting them, but since you want to point it out, yeah, i guess i only meant "after" the error. :roll:

Re: Call function before/after Fatal Error?

Posted: Mon Jan 11, 2010 1:17 pm
by flying_circus
You can catch *some* fatal errors with the use of output buffering. I'm not completely sure if this is exactly what you're looking for, but consider the following code:

Code: Select all

<?php
  function handle_error()
  {
    if(error_get_last())
    {
      return ("<pre>Caught Error: " . ob_get_contents() . "<br />We can gracefully handle some fatal errors.</pre>");
    }
  }
  
  # Begin Output Buffering
    ob_start('handle_error');
  
  # Cause a PHP Fatal Error:
    does_this_exist();
?>

Re: Call function before/after Fatal Error?

Posted: Mon Jan 11, 2010 1:20 pm
by infolock
Thanks flying_circus, that's close to what I have in place in a way. It just doesn't cover everything I need though =( which is kinda the situation. Oh well. guess I'll be waiting to see if PHP 6 does it or not for sure. Thanks for everyone's input!