Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

eval in eval

Posted on 2006-10-25
6
Medium Priority
?
541 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:bitfactory
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:anilande
ID: 17809819
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();\");
 }
0
 

Author Comment

by:bitfactory
ID: 17809917
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 ...

0
 
LVL 48

Accepted Solution

by:
hernst42 earned 200 total points
ID: 17810025
ever thought of using reflection ( http://de3.php.net/reflection ) for that kind or http://de3.php.net/manual/en/function.call-user-func.php

e.g.
private function DoAction()
 {
      echo("DoAction: <br/>");
      $val = $this->Action();
      call_user_func(array($this, $val));
 }
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:bitfactory
ID: 17810218
thanks hernst42 i'll check out the reflections... Didn't know of them before. I accept your answer as it was helpful.
0
 
LVL 4

Expert Comment

by:anilande
ID: 17810347
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;
}
0
 

Author Comment

by:bitfactory
ID: 17810387
Thanks anilande:) This would have worked as well :) But now I'm using call_user_func instead which looks more elegant.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question