Re: How would you rewrite this snippet without using eval?

From: Date: Mon, 26 May 2008 17:44:59 +0000
Subject: Re: How would you rewrite this snippet without using eval?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-274703@lists.php.net to get a copy of this message
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!


« previous php.general (#274703) next »