Let me be straight: I find the syntax of anonymous functions and closures in PHP annoying and disturbing. It may be clear and it may be easy to apprehend but it is way too long. Is this a real problem? In my opinion, it is.

Closures are a great weapon for every programmer. They offer a way to encapsulate not only data and states but also common tasks that appear in almost every program, such as filtering, mapping and null checking which I analyze below.

Yet, the syntax of closures is so long, that their application in such tasks make the code longer and less elegant.

In the following examples I am going to use some structures that I won’t define because their implementation is out of the scope of this article. I want to focus on the application of the structures, supposing that somebody has already done the hard work behind the scenes.

Case 1: Filtering

Suppose that you have a list of objects out of which you want to pick those that meet some criteria. Let’s say that your list is $a and you want to pick only the objects whose Name property is equal to $name.

Here is how you would do that in a traditional imperative way:

// PHP imperative way $result = array(); foreach ($a as $x) if ($x->Name == $name) $result[] = $x;

Here is how you would go in a functional way (using some smart data structure):

// PHP functional way $result = $a->Where( function($x)use($name){ return $x->Name == $name; });

As you can see the code is not at all shorter, let alone the fact that I had to write $name twice.

Can’t we have something better? Sure, here is the same code in C#:

// C# functional way result = a.Where(x => x.Name == name);

Case 2: Mapping

This is a common scenario. From a list of objects $a return a comma separated string of all Names.

// PHP imperative way $b = array(); foreach ($a as $x) $b[] = $x->Name; $result = implode(', ',$b);

Here is the functional equivalent, again with the help of some data structure:

// PHP functional way $result = $a->Select( function($x){ return $x->Name; }) ->Join(', ');

Once again this is not shorter or simpler…

Of course, in C#, the same code looks effortless:

// C# functional way result = a.Select(x => x.Name).Join(', ');

Case 3: Null checking

You know what I am talking about, don’t you? Straight into code:

// PHP imperative way $result = is_null($x) ? null : $x->Name;

The functional way would imply the use of some Maybe Monad:

// PHP functional way $result = $x->Select( function($x){ return $x->Name; } );

Once more, this is longer and more complex!

Of course, C# is once again better:

// C# functional way result = x.Select(x => x.Name);

Conclusion

In all three examples, the functional way has many advantages over the imperative one, because:

The desired behavior is abstracted away and therefore there will be less points of failure. The result is given from just an expression and not a block of statements. This makes it easier to apply more methods on top of it, in a continuation. The functional way is lazy.

However, the syntax of PHP’s closures is an obstacle. The programmer is given the choice between simple (but naive) and complex (but state of the art). It does not have to be that way, as C# has shown us!

Anonymous functions and closures have so many applications that I believe a more elegant syntax would benefit everyone.