Call function before/after Fatal Error?

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Call function before/after Fatal Error?

Post 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?
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Re: Call function before/after Fatal Error?

Post by Charles256 »

User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Call function before/after Fatal Error?

Post 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.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Re: Call function before/after Fatal Error?

Post by Charles256 »

What fatal errors are you experiencing that even brings this to mind if you don't mind me asking?
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Call function before/after Fatal Error?

Post 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...
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Call function before/after Fatal Error?

Post 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...
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Re: Call function before/after Fatal Error?

Post by Charles256 »

Ah. tell him to stop writing crappy code? :-D
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Call function before/after Fatal Error?

Post 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...
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Call function before/after Fatal Error?

Post 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:
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Call function before/after Fatal Error?

Post 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();
?>
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Call function before/after Fatal Error?

Post 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!
Post Reply