tags:

views:

653

answers:

2

Hi Stackers

I am trying to use this in my page class. I only just started using objects in PHP so I'm still a little clueless (but learning as much as I can). This is in my page() function (so called when there is a new instance of page)

set_error_handler('$this->appendError');

This is causing an error

Warning: set_error_handler() expects the argument (appendError) to be a valid callback

Now how do I set a class internal function whilst passing the function as a string. Is this not possible? Should I use a normal function which then calls the class function and sends through all arguments? This sounds a little cumbersome to me.

Or have I missed the problem? I've tried making my appendError return a string, and echo.. but it still isn't playing nice.

Any help would be greatly appreciated.

Thank you!!

+3  A: 

Few problems with that.

First:

'$this->appendError'
is a nogo. It doesn't interpret $this to the current class, php interprets it as the string '$this'.

Second: Try

set_error_handler(array($this, 'appendError'));

If that doesn't work, replace $this with the classname and use it statically.

Ray
It worked... Thank you very much. :)
alex
+1  A: 

Read the php.net callback documentation. I think example 3 is closest to what you want:

// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj, 'myCallbackMethod'));
yjerem
Just curious, can you do this with JavaScript using jQuery? i've always used function() { functionIWant($args) }
alex