开发者

Using dynamic methods

开发者 https://www.devze.com 2023-03-08 21:36 出处:网络
Why does my dynamic method usersMethod not return any result? The page is always empty开发者_StackOverflow.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消