Handling PHP errors not sent to custom handler

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
joeshmoe666
Forum Newbie
Posts: 7
Joined: Sat Sep 12, 2009 2:02 pm

Handling PHP errors not sent to custom handler

Post 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
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Handling PHP errors not sent to custom handler

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Handling PHP errors not sent to custom handler

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Handling PHP errors not sent to custom handler

Post by Eran »

joeshmoe666
Forum Newbie
Posts: 7
Joined: Sat Sep 12, 2009 2:02 pm

Re: Handling PHP errors not sent to custom handler

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Handling PHP errors not sent to custom handler

Post by Jenk »

josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Handling PHP errors not sent to custom handler

Post 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
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Handling PHP errors not sent to custom handler

Post 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]
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Handling PHP errors not sent to custom handler

Post by Darhazer »

PHP 5.2 generates HTTP 500 response if there is a fatal error and display_errors is off
Selkirk
Forum Commoner
Posts: 41
Joined: Sat Aug 23, 2003 10:55 am
Location: Michigan

Re: Handling PHP errors not sent to custom handler

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