Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using dynamic methods

Tags:

php

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();
?>
like image 252
lolalola Avatar asked Dec 20 '25 22:12

lolalola


2 Answers

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.

like image 34
mario Avatar answered Dec 22 '25 10:12

mario



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!