Showing posts with label simplicity php framework. Show all posts
Showing posts with label simplicity php framework. Show all posts

Friday, February 29, 2008

eval is still evil

Here is an interesting code snippet from the simplicity php framework:

//...
static public function check($path=false) {
if (!self::loadFiles($path)) return false;

$arr = "";
$apath = explode('.',$path);
foreach ($apath as $pt) {
$arr .= "['{$pt}']";
}

$var = 'self::$_settings'.$arr;
$eval = "return isset({$var}) ? true : false;";
return eval($eval);
}
//...


What happens? This code traverses through an multidimensional array with N dimensions.
Yes, recursion is sometimes tedious. But if i can avoid an eval call, i avoid it! The solution without eval is not longer and not much harder to understand and written in nearly the same time:

$data['foo']['baz']['wtf'] = 'whupass';
$path = 'foo.baz.wtf';

$p = explode('.', $path);
$value = $data;
while ($k = array_shift($p)) {
$value = $value[$k];
}

echo "$value\n";


Cute, isn't it?