Reputation: 67710
so far, I have this:
class Foo{
private $plugin_methods = array();
public function registerPlugin($caller, $method){
list($object, $caller) = explode('.', $caller);
$this->plugin_methods[$object][$caller] = $method;
}
public function _doPluginMethod($object, $name, $args){
if(isset($this->plugin_methods[$object][$name]))
return call_user_func_array($this->plugin_methods[$object][$name], $args);
throw new Exception("Method '{$name}' not defined for '{$object}'.");
}
public function __call($name, $args){
return $this->_doPluginMethod('foo', $name, $args);
}
}
and now I can do this:
$foo = new Foo();
$foo->registerPlugin('foo.my_plugin', function($something){
return $something * 1000;
});
$foo->my_plugin(3453245);
But how can I get to the $this
object inside my "plugin" function?
Upvotes: 1
Views: 187
Reputation: 24579
Depends on the version of PHP you are using. As of PHP 5.4, the use of $this
in anonymous functions (also called closures) is possible. Prior to that, it wasn't.
Check the changelog here: http://php.net/manual/en/functions.anonymous.php
Upvotes: 1