I/O Reader

RSS Feed

Peter Goodman's blog about PHP, Javascript, Functional Programming, Applications, Actionscript,

2½ Cool Hacks in PHP

posted on Jun 26, 2008 at 10:12am with no comments

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.

<?php
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:

<?php
$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.

Comments

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


Comment



write down what you see in the box below (all uppercase)