How would you rewrite this snippet without using eval?
| From: | Luigi Perroti | Date: | Mon, 26 May 2008 17:34:08 +0000 |
| Subject: | How would you rewrite this snippet without using eval? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-274702@lists.php.net to get a copy of this message | ||
Hi all,
I'm rewriting a script trying to avoid using functions that might pose
a security threat if used incorrectly.
I have a portion of code that looks like the one pasted below.
I don't seem to be able to avoid using eval without introducing too
many changes, losing in simplicity.
Here's the code:
<?php
echo Logic::getFirstValue(); // Output: first value
class Logic {
public static function getFirstValue() {
return Data::getData('$firstArray','firstKey');
}
}
class Data {
private static $firstArray = array ('firstKey'=>'first value');
private static $secondArray = array ('secondKey'=>'second value');
public static function getData($array, $key) {
$string = 'return self::'.$array.'[\''.$key.'\'];';
return eval ($string);
}
}
?>
How would you rewrite the snippet without using eval and still keeping
things very straightforward?
Thanks for your attention!