Using dynamic methods
Why does my dynamic method usersMethod
not return any result?
The page is always empty开发者_StackOverflow.
<?php
class SampleClass
{
public function __call($name, $args)
{
$m = $this->methods();
eval($m['usersMethod']);
}
public function methods()
{
$methods = array(
'usersMethod'=>'$a=2; return $a;',
'membersMethod'=>'$a=1; return $a;'
);
return $methods;
}
}
$sample = new SampleClass();
echo $sample->usersMethod();
?>
You need to return the value of eval
:
return eval($m['usersMethod']);
(See this answer)
You have an return
statement in the code snippets that are to be used as "functions". But this return only sends the value over the eval(). You need to return the eval result as well:
return eval($m['usersMethod']);
Only then will the internal $a be returned via the __call() method invocation.
精彩评论