Solved

eval in eval

Posted on 2006-10-25
6
528 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 

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

Industry Leaders: 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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

756 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