note 26029 added to function.eval
| From: | shamim at poetryunlimited dot com | Date: | Tue, 15 Oct 2002 16:10:59 +0000 |
| Subject: | note 26029 added to function.eval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-38120@lists.php.net to get a copy of this message | ||
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"; }
}
--
http://www.php.net/manual/en/function.eval.php
http://master.php.net/manage/user-notes.php?action=edit+26029
http://master.php.net/manage/user-notes.php?action=delete+26029
http://master.php.net/manage/user-notes.php?action=reject+26029