Page 1 of 1

Handling PHP errors not sent to custom handler

Posted: Wed Sep 16, 2009 4:13 pm
by joeshmoe666
So I understand that certain PHP errors don't get sent to a custom handler, for instance calling a non-existent function generates an E_COMPILE_ERROR causing execution to halt. So is there any other way to handle this other than the system coming to a halt? Is there some way to use Apache to trap this kind of PHP error and display a custom page, or do these errors not bubble up thru Apache.

Also curious .. I'm not using OOP, but does it handle all errors better than procedural programming with functions, or is it the same ??

Thanks in advance

Re: Handling PHP errors not sent to custom handler

Posted: Wed Sep 16, 2009 4:52 pm
by PHPHorizons
I am not aware of anything in apache that allows it to show custom error pages based on which fatal error php encounters; although I'm sure there's a way somewhere.


But, if this is production code, it should never encounter a fatal error. PHP certainly wasn't designed to accommodate what you're trying to do. PHP simply gives up on a fatal error.

If you think a function call may be made to a nonexistent function, then wrap that function call in a "function_exists()" call so that you can handle the "non-existent" function call (attempted, not actually executed) within your code.

Every instance of a fatal error can be defensively programmed for.


As for oop vs procedural in php: php is first and foremost a procedural language. You can do everything in a procedural manner. Even if you're calling methods on built in php objects, your code can still be procedural. In the case of error handling, some obscure php extension might have Exceptions thrown in them, but I've never had one thrown that I didn't throw, which means that all errors in php can be handled just as well from a procedural standpoint.

I do recommend the oop route though :P

Hope that helps.

Re: Handling PHP errors not sent to custom handler

Posted: Wed Sep 16, 2009 5:19 pm
by jackpf
ALL error messages are sent to the error handler.

However, if you're defining the error handling function in a file that has a parse error...then it's not going to define the error handler...so it'll show up as a normal error.

Re: Handling PHP errors not sent to custom handler

Posted: Wed Sep 16, 2009 5:37 pm
by Eran

Re: Handling PHP errors not sent to custom handler

Posted: Wed Sep 16, 2009 5:48 pm
by joeshmoe666
Thanks guys .. very helpful, although not what I was hoping.. I don't think apache's error document works because the PHP errors do not appear to bubble up to apache. I tried looking in Apache's error log and it appeared that PHP was generating an error 200, but when I created a doc for it, Apache didn't capture the error ..

Thanks again

Re: Handling PHP errors not sent to custom handler

Posted: Thu Sep 17, 2009 3:35 am
by Jenk

Re: Handling PHP errors not sent to custom handler

Posted: Thu Sep 17, 2009 10:15 pm
by josh
Jenk wrote:HTTP 200 isn't an error.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
Unless your client's spec says that page is to return http 404 :drunk:

You should disable display errors, turn on logging, and pro-actively test your code though

Re: Handling PHP errors not sent to custom handler

Posted: Fri Sep 25, 2009 10:37 pm
by Ollie Saunders
Every error that can be handled within a single PHP script will be sent to a custom handler, if set. You might have to get pretty creative to get around that limitation. Execution continues if parse errors are encountered in eval()'d code, although not errors caused by undefined functions:

Code: Select all

$ php -a
Interactive shell
 
php > eval('xyz();')
php > ;
 
Fatal error: Call to undefined function xyz() in php shell code(1) : eval()'d code on line 1
[b]$[/b]

Code: Select all

$ php -a 
Interactive shell
 
php > eval('class;');
 
Parse error: syntax error, unexpected ';', expecting T_STRING in php shell code(1) : eval()'d code on line 1
php > echo 1 + 1;
2
[b]php >[/b]

Re: Handling PHP errors not sent to custom handler

Posted: Sat Sep 26, 2009 8:07 am
by Darhazer
PHP 5.2 generates HTTP 500 response if there is a fatal error and display_errors is off

Re: Handling PHP errors not sent to custom handler

Posted: Mon Sep 28, 2009 10:06 pm
by Selkirk
You can use register_shutdown_function. The shutdown function gets called even for fatal errors. So you have to set a global flag so that you can tell an abnormal shutdown from a normal shutdown. You won't get the information that the error handler will get, but you can log some information. Or just let PHP generate a 500 error for you.