Re: How would you rewrite this snippet without using eval?
| From: | n3or | Date: | Mon, 26 May 2008 17:47:22 +0000 |
| Subject: | Re: How would you rewrite this snippet without using eval? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-274704@lists.php.net to get a copy of this message | ||
Sorry, I forgot, if you have to use the $ in the getData Parameter, use:
$array = substr($array, 1);
before the return statement.
Greetings
n3or
n3or schrieb:
Like this: <?php echo Logic::getFirstValue(); // Output: first value class Logic {public static function getFirstValue() { return Data::getData('firstArray','firstKey'); // without $ }} class Data {private static $firstArray = array ('firstKey'=>'first value'); private static $secondArray = array ('secondKey'=>'second value'); public static function getData($array, $key) { return self::${$array}[$key]; }} ?> Greetings n3or Luigi Perroti schrieb: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!