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?

deconstructing xyster

I've stumpled across a new framework called xyster and it's not surprising that behind the fancy web 2.0 website is lurking something evil.
Xyster is an extension to Zend Framework and extends ACL, MVC with some features and uses the Database Layer for an ORM package. So what is the evil in xyster? Evil is the Xyster_Collection package. Everyone who knows Java's collection mess. Everyone who envied languages with object oriented array's. Will have some (good or bad) emotion now. But this is not the point. The points are interoperability and simplicity. It isn't easy nor fun to convert from/to collection Foo, again and again. Indeed, in Java this is common, but Java sucks exactly for this reason (and some others)! PHP does better, just hand over some native array's that work everywhere. And if you really need a fancy collection-like-something, create an highly specialized version and integrate it in the specific domain.

There is one small chance to create a collection framework for PHP that is interoperable. Write an RFC. Write an native extension. Start an petition. Pray. An don't forget to become a landowner, in case rasmus comes around and kills you with a roundhouse kick.

This fact would be a great disadvantage, if i had a need for xyster (or any other framework with the same flaws).