lolalola
lolalola

Reputation: 3823

Using dynamic methods

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();
?>

Upvotes: 3

Views: 171

Answers (2)

mario
mario

Reputation: 145482

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.

Upvotes: 2

user142162
user142162

Reputation:

You need to return the value of eval:

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

(See this answer)

Upvotes: 4

Related Questions