Solved

eval in eval

Posted on 2006-10-25
6
527 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 50 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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.

831 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