Using set_error_handler within a class

I have an error class and I would like to include my custom error handler within that class. My class name is


The only problem is when I tried to call set_error_handler in my core file, it doesn’t seem to recognize that. I tried calling it like this

set_error_handler(("cms->error", "cr_error_handler"));

I know i’m doing something wrong but I"m not sure how to correctly set the error handler so it works inside the class

set_error_handler((&$this, "method_name"));

Read the user comments.

Are you forgetting the “array” portion?
to use classes/objects as callback:
array( $objinstants|'classname', 'methodname' )

PHP 5 you don’t need “&” at all when dealing with objects.

Yes, I did forget :slight_smile:

A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.

Static class methods can also be passed without instantiating an object of that class by passing the class name instead of an object at index 0.

Apart from common user-defined function, create_function() can also be used to create an anonymous callback function.

That sort of worked. I needed to add the array part but I also could not use &$this since I was outside the class I was using. If I put the set_error_handler inside the error class than that worked. I think that’s what I will do



class TestErrorHandling
	protected $error;

	public function __construct ()
		$this->error = new TestErrorHandler;
		set_error_handler( array( $this->error, 'execute' ) );

class TestErrorHandler
	public function execute ()
		var_dump( 'Error!' );

$c = new TestErrorHandling();
print $b; # On purpose!
// Comment bug fix comment