capturing "Fatal error: Call to undefined function xxx&

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mechamecha
Forum Commoner
Posts: 32
Joined: Thu May 31, 2007 8:49 pm

capturing "Fatal error: Call to undefined function xxx&

Post by mechamecha »

Hello,
I'm writing my own error logging handler function that writes php errors to disk. I cannot figure out how to capture errors that come from calling an undefined function or malformed php statements. If I set php to display all errors, I can see these errors displayed on my browser, however these errors don't appear to invoke my error handler function.

Here are the php calls I'm making to setup my error logger:

Code: Select all

ini_set( 'error_reporting', E_ALL );
ini_set( 'display_errors', 'Off' );
ini_set( 'log_errors', 'On' );

// the error logging function
function myErrorHandler( $p_number, $p_string, $p_file, $p_line, $p_context )
{
// ...
}

set_error_handler( 'myErrorHandler' );
Am I missing something here?

THanks
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.
mechamecha
Forum Commoner
Posts: 32
Joined: Thu May 31, 2007 8:49 pm

Post by mechamecha »

Thanks for the heads up Hockey!

In that case, is there any way to get these particular error types logged to disk? Can I get the default error logger to log these errors to disk?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

I have to ask why you would want to log that kind of error...compile time errors are really only of interest once and should be fixed ASAP and forgotten.

Missing functions are possibly somethign worth logging, in the case where you have dynamically loaded modules, etc...

In this case, I wouldn't rely on error logging but rather a conditional check to see if the function/method/class exists and if not, log the error and throw an exception...

Code: Select all

$function = 'do_something';
if(function_exists($function)){
  // Do something
}
else{
  echo "Function: $function didn't exist";
}
mechamecha
Forum Commoner
Posts: 32
Joined: Thu May 31, 2007 8:49 pm

Post by mechamecha »

Yeah, normally I wouldn't care to log such errors. However, recently I had to spend a whole day tracking a bug due to a missing function. The problem was really due to the fact that my development environment was running a later version of php than my production environment. A rookie mistake, but having php log an error to disk for an unrecognized function would have been nice.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Set the error_log configuration setting.
Post Reply