Closures And Lambda Functions In PHP (Programming)

Paul M. Foster (01/28/16 16:26:40 Revision 0)

I suppose it had to happen some time. Someone decided to take one of the worst mis-features in Javascript and graft in onto PHP. I'm talking about lambda functions.

Lambda functions (and closures) are a relatively new feature of PHP which has existed for some time in Javascript. A lambda function is more or less an anonymous function. That is, a function without a name. You can assign such a function to a variable, for example, and when the variable is called, the function fires. They can also be passed to another function as an argument. In this case, the whole function definition appears in between the paratheses after the name of a different function, as:

function show_welcome(function () {
echo 'Welcome!';
});

The code above is typical of that used to describe and explain how lambda functions are used in function calls. You can see that the whole function is defined just like any other function except for two things: first, it has no name where the name would normally be, and the whole thing is in the argument space of the original "show_welcome" function. Or, if you prefer, on the stack.

I'd like to point out something here. The following performs the same actions, but in a more traditional format:

function show_welcome()
{
	echo 'Welcome!';
}

Which is easier to read and understand? Which might be easier to debug?

You could also do the following, if you just had to make the code more complex:

function my_welcome()
{
	echo 'Welcome!';
}

function show_welcome() { my_welcome(); }

Pretty silly, eh?

Lambda functions are used extensively in Javascript, not because the language dictates them, but because it allows them, and programmers apparently consider themselves kewler when they use them. Let's face it, if you're going to write a bunch of one-off code only used by one function, why put the whole block of code on the stack of the function? Why not just do the obvious and stick that section of code in the function as the first thing it does? I can't think of a good reason.

"Closures" are identical to lambda functions except they can be use variables which would normally be beyond the scope of regular anonymous functions.

Now your next question might be, why would anyone prefer and pursue this practice and why would language architects build in this capability? Good question. The only semi-viable answer I've heard is that such functions are one-offs, and that writing them this way conserves the global name space. That is, they don't "pollute" the global name space. Of course, if the code is one-off and only used in one function, it's just as easy to insert the code as the first part of the function interior, and a helluva lot easier to read.

Yes, you heard right. Let's step back for a moment and examine what PHP and Javascript are used for. Both languages are used to display and operate a single web page at a time. That's one screen. One. An accounting package written in C may end up displaying a hundred different screens, and be on in the background for weeks or months at a time. Not so, PHP and Javascript. These are web pages. They display information, and/or offer choices within the framework of a single page. You can get fancy with Javascript and have it radically change the look of a page, but it's still just one page of a website. Now, if you have a language like C and a huge block of code like an accounting package, which is resident in memory for days or weeks at a time, I can maybe understand worrying about the name space, which can get quite crowded with function names and variables. But we're talking, in PHP and Javascript, about a single page. One screen.

So this one specious argument for Lambdas and closures doesn't hold up for either PHP or Javascript. I mean, seriously, if you're going to drag so much code around with you to paint one web page that you're actually worried about how crowded your global name space is, you need to seriously re-think how much code is getting dragged around and not used. Moreover, if you're using OOP, a great many of your function names should be hidden from the global scope by being part of a class. And again, a lot of the lambda functions I've seen are short enough that they could just as easily be written as part of the function whose argument space they're being defined in. Moreover, PHP recently added namespaces to the language, so protecting the namespace may be accomplished that way instead.

Whatever possessed the architects of PHP to include this misfeature in the 5.3 version of the language is beyond me. I suppose they were playing, "Keep up with the Javascript Joneses", since a lot of PHP coders also code in Javascript. Hopefully, in practice, cooler heads will prevail. But I rather think not, since lambdas are used so extensively in Javascript. This is one of the primary reasons I eschew Javascript. (Yes, I know that PHP and Javascript are used for different purposes and contexts.)

Going forward, I implore programmers to avoid lambdas and closures, since they appear to offer no clear advantage and they make code hard to read, understand and debug. (Seems I've heard the argument here and there that lambdas actually make debugging easier. I've debugged an awful lot of code, and I seriously doubt it.)

I hope you see what I'm getting at, and can see the logic. Let this misfeature of PHP and Javascript quietly die from disuse.

Add Comment:

*
*