Anonymous (Lambda) functions in php 5.3 and above. 

One thing I love about javascript is the anonymous functions it provides.
It allows you to create and use one-time functions that you don’t have to ever see again, use or think how to name in order not to mess with your pretty coding standards.

I found a post by Daniel Cousineau on php5.3′s closures that lead me to the closures rfc.

Anonymous (or lambda) functions are extremely useful for callbacks or an quick and dirty way to create extensions or overrides for your code.
The examples are stolen from the closures rcf.

$lambda = function () { echo "Hello World!"; };

That was it. Now you can call it anyway you want.

$lambda ();
call_user_func ($lambda);
call_user_func_array ($lambda, array());

The patch is actually created to allow closures support, something also very common in javascript.
Closures are used to change the function’s scope so it can access specific variables that normally the function would not have access to.

The examples in the rcf are pretty simple but get the point across.
The following example is something I didn’t thing would work, but I still ain’t sure how the lambda functions’ scope works.

class something
{
	public $hello = 'Hello world!';

	public function world()
	{
		$x = function () { return $this->hello; };
		return $x();
	}
}

$s = new something();
echo $s->world();

This actually shows “Hello world!”…
The weird part is that the function acts like it is a method of the something class and can see $this.
If you were to make a normal function it wouldn’t have access to $this.
I’ve submitted a bug report about this but I’m pretty sure it’s a feature since you can’t use $this in a closure.

Update: The reply to the bug report was:

This is the intended behavior. Use “static function” if you don’t need $this.

So there goes… ;)