note 104364 added to reflectionfunction.invoke
| From: | spark at limao dot com dot br | Date: | Fri, 10 Jun 2011 17:55:54 +0000 |
| Subject: | note 104364 added to reflectionfunction.invoke | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-180428@lists.php.net to get a copy of this message | ||
I know Reflections classes have a lot of power, but sometimes all we need is to store a annonymus
function or even create a simple callback somewhere.
so here it is, the Callback class:
<?php
class Callback{
private $name = false;
public function Callback($obj,$call=false){
$name = array($obj);
if($call) $name[] = $call;
$this->name = $name;
}
public function invoke($params=array()){
return call_user_func_array($this->name,$params);
}
}
?>
Usage:
<?php
function sayName(){
return "goku";
}
$myVar = new Callback("sayName");
echo "Hi, I am ".$myVar->invoke()."!";
//also works with methods: new Callback($obj,"method");
?>
----
Server IP: 69.147.83.197
Probable Submitter: 187.55.215.170
----
Manual Page -- http://www.php.net/manual/en/reflectionfunction.invoke.php
Edit -- https://master.php.net/note/edit/104364
Del: integrated -- https://master.php.net/note/delete/104364/integrated
Del: useless -- https://master.php.net/note/delete/104364/useless
Del: bad code -- https://master.php.net/note/delete/104364/bad+code
Del: spam -- https://master.php.net/note/delete/104364/spam
Del: non-english -- https://master.php.net/note/delete/104364/non-english
Del: in docs -- https://master.php.net/note/delete/104364/in+docs
Del: other reasons-- https://master.php.net/note/delete/104364
Reject -- https://master.php.net/note/reject/104364
Search -- https://master.php.net/manage/user-notes.php