Link to home
Start Free TrialLog in
Avatar of bitfactory
bitfactory

asked on

eval in eval

Hi All,
I'm having a issue with evel() in PHP. I have a class, which has a function DoAction. This function shall use eval() in order to run some objects function according to what action was set while object was constructed. Here's how I'm doing it now and how it works:

 private function DoAction()
 {
       echo("DoAction: <br/>");
       eval("\$val = \$this->Action();");
       eval("\$this->$val();");
 }

However I was wondering if it is possible to put it in one eval. I was trying something like:

 private function DoAction()
 {
       echo("DoAction: <br/>");
       eval("\$val = \$this->Action();");
       eval("\$this->eval(\"return \$this->Action();\");");
 }

but this thins (logically) that the eval() inside the eval() is the objects function, which is not defined ...

Is there a way how to make this or shall I use that two eval() solution?

Thanks
Avatar of anilande
anilande

if i am not wrong u want to achive some thing bellow

 private function DoAction()
 {
      echo("DoAction: <br/>");
      eval("return \$val=\$this->Action();\");
 }
 or if u don't want to store it in $val then u can use the following

 private function DoAction()
 {
      echo("DoAction: <br/>");
      eval("return \$this->Action();\");
 }
Avatar of bitfactory

ASKER

Hi,
maybe I didn't tell it clearly. The thing you wrote will return only a action string. the function $this->Action() returns a string which is a name of the function to be executed.

So in fact i want to call the function which name is returned by $this->Action();

e.g. $this->Action returns "test" so I want to call function $this->test();

the thig you wrote only returns the string "test" and doesn't call the function ...

ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks hernst42 i'll check out the reflections... Didn't know of them before. I accept your answer as it was helpful.
sorry i am late
but i found an answer

try this out

private function DoAction()
{  
    echo("DoAction: <br/>");
    @eval("\$val=\$this->".eval('return \$this->Action();')."();");
    return $val;
}
Thanks anilande:) This would have worked as well :) But now I'm using call_user_func instead which looks more elegant.