3

Why does my dynamic method usersMethod not return any result? The page is always empty.

<?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();
?>
1
  • You should really change __call() to use the name and args passed in instead of hard-coding it. Commented May 29, 2011 at 14:36

2 Answers 2

4

You need to return the value of eval:

return eval($m['usersMethod']);

(See this answer)

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.