PHP, create_function or evalute it at runtime?
Posted
by blow
on Stack Overflow
See other posts from Stack Overflow
or by blow
Published on 2009-10-23T09:13:22Z
Indexed on
2010/05/03
8:58 UTC
Read the original article
Hit count: 299
Hi all, i have a class with some method that depend by one parameter. What is the best way to write this method?
Example:
First way
class Test{
var $code;
function Test($type){
if($type=="A"){
$this->code=create_function(/*some args and some code*/);
}
else if($type=="B"){
$this->code=create_function(/*some args and some code*/);
}
}
function use(/*some args*/){
return call_user_func($this->code,/*some args*/);
}
}
Second way
class Test{
var $type;
function Test($type){
$this->type=$type;
}
function use(/*some args*/){
if($this->type=="A"){
//some code
}
else if($this->type=="B"){
//some code
}
}
}
$test=new Test("A");
$test->use();
Which way you would choose?
Thanks.
© Stack Overflow or respective owner