Jump to content

Error handling


sunilvadranapu

Recommended Posts

Hi,

  I am writing a PHP class for custom error handling. In constructor i am setting up the error handler like this.

    function __construct (){
    {
	if (error_reporting() == 0)
		return;

	$this->logger = new Logger("mylog.log","append") ;  //create object to logger class
	set_error_handler(array($this,"ErrorHandler"));        //set user defined error handler
    }

 

while running the i am getting below warning:

 

Warning: set_error_handler() expects the argument (Error::ErrorHandler) to be a valid callback in C:\php_dev\errorHandler \sun_error.php on line 16

 

 

what is this waring? can any one tell me how can i remove this warning??

 

thanks in advance

 

 

-sun

Link to comment
Share on other sites

PHP doesn't WANT an array for that function:

 

mixed set_error_handler  ( callback $error_handler  [, int $error_types  ] )

 

You need to just pass a function name.  Make a function that calls a Logger object, and then use that function for the set_error_handler.  You need to be a bit creative for it to work though.

Link to comment
Share on other sites

thanks for your reply.

 

In my "Error" class i have constructor and ErrorHandler() method.

 

Class: Error

    constructor

    ErrorHandler

 

i changed the constructor code to set user defined error handler to Error Class method "ErrorHandler"

 

set_error_handler("ErrorHandler");

 

even then i am getting the warning msg:

 

Warning: set_error_handler() expects the argument (ErrorHandler) to be a valid callback in C:\php_dev\errorHandler\sun_error.php on line 16

 

 

 

thanks in advance

Link to comment
Share on other sites

thanks for your reply.

 

In my "Error" class i have constructor and ErrorHandler() method.

 

Class: Error

    constructor

    ErrorHandler

 

i changed the constructor code to set user defined error handler to Error Class method "ErrorHandler"

 

set_error_handler("ErrorHandler");

 

even then i am getting the warning msg:

 

Warning: set_error_handler() expects the argument (ErrorHandler) to be a valid callback in C:\php_dev\errorHandler\sun_error.php on line 16

 

 

 

thanks in advance

 

Make a function SEPARATE of the class that creates a new Error object (sorry, I thought you named it Logger when I posted my previous post), and use THAT function as the error handler, and work with the object in it.

Link to comment
Share on other sites

 

This is my actual class:

 


<?php
class Error
{

    function __construct (){
    {
	if (error_reporting() == 0)
		return;

	//set_error_handler(array($this,"ErrorHandler"));
	set_error_handler("ErrorHandler");
    }
  
    function ErrorHandler($errno,$errstr,$errfile,$errline)
    {

	$errorType = array (
		   E_ERROR          => 'ERROR',
		   E_WARNING        => 'WARNING',
		   E_PARSE          => 'PARSING ERROR',
		   E_NOTICE         => 'NOTICE',
		   E_CORE_ERROR     => 'CORE ERROR',
		   E_CORE_WARNING   => 'CORE WARNING',
		   E_COMPILE_ERROR  => 'COMPILE ERROR',
		   E_COMPILE_WARNING => 'COMPILE WARNING',
		   E_USER_ERROR     => 'USER ERROR',
		   E_USER_WARNING   => 'USER WARNING',
		   E_USER_NOTICE    => 'USER NOTICE',
		   E_STRICT         => 'STRICT NOTICE',
		   E_RECOVERABLE_ERROR  => 'RECOVERABLE ERROR'
	);


	$errtime=date("Y-m-d H:i:s (T)");
	$err_message="An error occured on ".$errtime."\n";
	$err_message .="Details are as follows\n";
	$err_message .="Error Number : $errno\n";
	$err_message .=$errstr."\nOccured in ".$errfile."\n";
	$err_message .="On Line $errline \n\n";
	$err_message .=str_repeat("-",50);
	$err_message .="\n";
       
}



}  //class
?>	

Link to comment
Share on other sites

 

My intension is to write a PHP class for handling errors. thats why i am developing the "Error" class which has a constructor in which i am setting up the user defined error handler and a "ErrorHandler" method which handles all errors encountered while execution.

 

please help me in this regard

 

 

thanks in advance

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.