1

Good day! Currently i'm studying a class which has a following line:

if( $something) {  eval ('$actualValue = &$this->'.$value.';'); }

Could you explain to me pro & cons of using eval and writing it without eval (just assign a value via a php regularly )

UPDATE 1

Sorry for this, but i just saw in the other part of the class a more complete version of using eval:

if( $something) {  
eval ('$actualValue = &$this->'.$value.';'); 
if(isset($actualValue)) { 
//some code
}
}

It's clearer now... but still there's a question : why not do it without eval... just don't get it. But still thanks everyone for your answers & giving links!

Elmor
  • 4,775
  • 6
  • 38
  • 70
  • possible duplicate of [Question about eval in PHP 5](http://stackoverflow.com/questions/2482861/question-about-eval-in-php-5) – Ja͢ck Jun 08 '12 at 14:42
  • Jack, I don't think this is the same question at all. They are both about eval, but the similarities ends there. – Emil Vikström Jun 08 '12 at 14:49
  • Check this [thread](http://stackoverflow.com/questions/951373/when-is-eval-evil-in-php) on more information how and when to use eval, really good thread – greenLizard Jun 08 '12 at 14:43

2 Answers2

5

No, there's no benefit to this. This is much better:

$actualValue = &$this->$value;

I think someone didn't know you could do this and so they reinvented the wheel.

Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
1

Take a look at the following question I asked:

Dynamically Populating Multi-Dimensional Arrays

I used eval to create a dynamic multidimensional array based on parameters tossed via a function argument.

But, it is much easier to just store variables via reference.

Community
  • 1
  • 1
Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87