/* there’s no place like code */

call methods of objects crated on-the-fly in PHP

Originally posted at saji-codes tumblr.

PHP sux, that’s not news.

I always wanted to do this:

(new Object())->method();

but it did not allow me. But there’s a neat trick, which makes it possible. Kind of. Just define small helper function:

function o($o){return $o;}

and now:

o(new Object())->method(); // almost like (new Object())->method();

voila!

You can also pretend to refer to array keys of arrays created on-the-fly.

function k($a, $k){return $a[$k];}
k(array_flip($array), 'key'); // almost like array_flip($array)['key']

Update

Newer versions of PHP allows that syntax.