Solved

eval in eval

Posted on 2006-10-25
6
524 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
Comment Utility
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
Comment Utility
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 50 total points
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:bitfactory
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks anilande:) This would have worked as well :) But now I'm using call_user_func instead which looks more elegant.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now