note 26029 deleted from function.eval by victor
| From: | victor@php.net | Date: | Sat, 12 Jun 2004 22:37:58 +0000 |
| Subject: | note 26029 deleted from function.eval by victor | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-70995@lists.php.net to get a copy of this message | ||
Note Submitter: shamim@poetryunlimited.com
----
A better way to implement the previous example - N.B. Eval is not REQUIRED for something with a hard
interface like this pattern. call_user_func is an alternative as is call_user_func_array for those
functions that need parameters as well. Eval is a last ditch resort for this pattern.:
class Processor {
function Processor( $cmd )
{
static $callbacks =
array(
"add"=>"add",
"cha"=>"change",
"del"=>"delete",
"new"=>"add",
"mod"=>"change",
"rem"=>"delete"
);
$proc = $callbacks[$cmd];
if( $proc )
$this->$proc();
// Alternatives include
// call_user_func(array(&$this,$proc));
// call_user_func_array(array(&$this,$proc),array());
}
function add() { echo "add"; }
function change() { echo "change"; }
function delete() { echo "delete"; }
}