2½ Cool Hacks in PHP

As I've been testing out lot's of things for my framework, I've come across a few neat little hacks. The first 1.5 hacks involves PHP's list() construct, and possible ArrayAccess as well.

list($foo->bar, $foo) = array(new Bar, new Foo);

Look closely at the order of variables in the list(). It seems that list actually sets the variables from right to left. It also indexes into the array numerically, which means that if instead of putting an array as the rvalue of list, you can put an object that implements ArrayAccess.

The second fun hack uses PHP's ArrayAccess interface (as before) but to overload PHP's array push syntaxt. Consider this valid piece of code that in PINQ merges an array into a dictionary:

$dict = new Dictionary;
$dict[] = array('foo' => 'bar');

This hack works using ArrayAccess::offsetSet, and recognizing when a NULL key is passed and handling it accordingly.


There are no comments, but you look like you have something on your mind.