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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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!



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux