|
-
Jun 15th, 2010, 11:33 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Callback Functions
I'm having an issue with callback functions, specifically storing them...
If I have a class:
PHP Code:
class myObject{
protected $callback;
public function __construct($callbackFunction){
$this->callback = $callbackFunction;
.
.
.
}
public function doSomething($args){
if(is_callable($this->callback)){
return $this->callback($args);
}
}
}
And I try doing this:
PHP Code:
function foo($arg){
return strtoupper($arg);
}
$myNewObject = new myObject("foo");
echo $myNewObject->doSomething("bar"); //should echo BAR
I end up with an error:
Code:
Fatal Error: Call to undefined method MyObject::callback() in ...
The is_callable() is succeeding, but the actual callback function call is the line failing with that error.
This isn't exactly what I'm doing obviously, but its a replicate of the code. Any ideas?
-
Jun 16th, 2010, 12:04 AM
#2
Re: Callback Functions
you have to tell PHP that you're calling $this->callback (the variable), not the method callback() of $this.
PHP Code:
return {$this->callback}();
-
Jun 16th, 2010, 12:28 AM
#3
Thread Starter
Hyperactive Member
Re: Callback Functions
PHP Code:
return {$this->callback}(empty($this->buffer) ? $template : $this->buffer);
It's not liking that.. unexpected {
I wanna stray from call_user_func() too, because that is hugely processor intensive.
Last edited by tomcatexodus; Jun 16th, 2010 at 12:34 AM.
IWS
-
Jun 16th, 2010, 12:35 AM
#4
Re: Callback Functions
oh. store it in a non-class variable then:
PHP Code:
$callback = $this->callback; $callback();
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|