note 103889 modified in function.call-user-func by danbrown
| From: | danbrown@php.net | Date: | Wed, 11 May 2011 15:51:54 +0000 |
| Subject: | note 103889 modified in function.call-user-func by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-179639@lists.php.net to get a copy of this message | ||
I made a wrapper for call_user_func_array which is really simple and convenient. It takes advantage
of the __invoke() magic method :
<?php
class Func {
public static function fromFunction($name){
return new Func($name);
}
public static function fromClassMethod($class, $name){
return new Func(array($class, $name));
}
public static function fromObjectMethod($object, $name){
return new Func(array($object, $name));
}
public $function;
public function __construct($function) {
$this->function = $function;
}
public function __invoke(){
return call_user_func_array($this->function, func_get_args());
}
}
?>
Example :
<?php
$func = Func::fromObjectMethod($myArrayObject, "offsetGet");
echo $func(3);
?>
--was--
I made a wrapper for call_user_func_array which is really simple and convenient. It takes advantage
of the __invoke() magic method :
class Func {
public static function fromFunction($name){
return new Func($name);
}
public static function fromClassMethod($class, $name){
return new Func(array($class, $name));
}
public static function fromObjectMethod($object, $name){
return new Func(array($object, $name));
}
public $function;
public function __construct($function) {
$this->function = $function;
}
public function __invoke(){
return call_user_func_array($this->function, func_get_args());
}
}
Example :
$func = Func::fromObjectMethod($myArrayObject, "offsetGet");
echo $func(3);
http://php.net/manual/en/function.call-user-func.php