Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I am beginning the process of moving code over to PHP 5.3 and one of the most highly touted features of PHP 5.3 is the ability to use closures. My understanding of closures is that they allow anonymous functions, can be assigned to variable names, and have interesting scoping abilities.

From my point of view the only seeming benefits in real world applications is the reduction of clutter in the namespace because closures are anonymous.

Am I wrong in this?

Should I be trying to put closures wherever I code?


I have already read this post on Javascript closures.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

First off: Closures are not everything you describe as such. The concept of functions as first-class objects is related - although pretty much a prerequisite for closures - and anonymous functions are not really related (you usually have both in the same languages, but still, neither is required for the next).

I have come to the conclusion that to appreciate closures and to put them to good use, one needs to adopt a slightly more functional mindset. For example: When using higher-order functions (functions that return functions), the need for closures arises quite often (basically, every time the returned function needs to access the higher order function's arguments) and they are in fact the most natural solution. If you're writing a class to serve as an accumulator factory instead of a higher-order function, of course the closure won't fit in.

As a concrete example: I'm just writing a (small-ish, limited, kind-of) parser generator for XML defined in some relatively weird (but luckily, simple - XML abused as database, the kind of job JSOn or YAML does better) XSD offshot. The parsers returned are functions and about every one of the parsers is a closure (closing over the schema representation by which it parses). I'd rather not type up classes for all of this.

share|improve this answer
I'm not sure why I would create functions that would return functions. That may be why I'm not seeing the benefit? – Patrick Feb 1 '11 at 17:01
@Patrick: Yes. As I wrote, seeing the point in closures (or most other tools from the functional programming world) requires some getting-used-to-it and practice to learn how to apply them. Of course it does - can a programmer who doesn't know the basics of OOP see the benefit of inheritance? No. Likewise, if you have little/no experience with functional programming, you propably can't apply closures. Perhaps you should learn a functional language? – delnan Feb 1 '11 at 17:04
ok, that makes sense. I just wanted to make sure I wasn't missing the point, and so I guess I'll read up on functional programming to get up to speed. thanks – Patrick Feb 1 '11 at 18:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.