6

There is a tag although there is no description of this tag so I didn't find it until I searched through the tags DB.

I did some research to understand closures, and I came across a good post to explain why this extra tag is needed.

Quote from PHP.net user:

This caused me some confusion a while back when I was still learning what closures were and how to use them, but what is referred to as a closure in PHP isn't the same thing as what they call closures in other languages (E.G. JavaScript).

In JavaScript, a closure can be thought of as a scope, when you define a function, it silently inherits the scope it's defined in, which is called its closure, and it retains that no matter where it's used. It's possible for multiple functions to share the same closure, and they can have access to multiple closures as long as they are within their accessible scope.

In PHP, a closure is a callable class, to which you've bound your parameters manually.

It's a slight distinction but one I feel bears mentioning.

So can I please get some help so I can update it?

I am currently struggling to really define it for the tag. I am happy to post the tag update myself obviously.

2
  • You can add a description (aka a tag excerpt or tag wiki) by clicking "edit" on the popup that appears when you hover over the tag. Are you asking for help for what to write?
    – Laurel
    Commented Aug 16, 2016 at 17:53
  • Yes thats exactly it, sorry if the post doesn't make that too clear. Commented Aug 16, 2016 at 18:49

2 Answers 2

1

There are two parts to a good tag excerpt:

  • How to use the tag
  • What the tag is about

In this case, you have a tag that is unlikely to be abused, so the first part will likely be nothing more than "Use for questions about closures in PHP". (This part can probably be omitted, since it's heavily implied.)

The second part should be a very brief summary of what the tag is about, PHP's closures. Ideally, it is just enough information that a random editor will be able to determine when to use the tag and when to remove it.

I suggest that you take a little time to read about closures. I suggest reading this answer, and using this PHP specific answer to help tie it back to PHP.

You could use a quote as long as you properly attribute the source. Note that you will not be able to use formatting (HTML or Markdown) in the excerpt.

0

A lambda is just an anonymous function - a function defined with no name. In some languages, such as Scheme, they are equivalent to named functions. In fact, the function definition is re-written as binding a lambda to a variable internally. In other languages, like Python, there are some (rather needless) distinctions between them, but they behave the same way otherwise.

A closure is any function which closes over the environment in which it was defined. This means that it can access variables not in its parameter list. Examples:

def func(): return h
def anotherfunc(h):
   return func()

This will cause an error, because func does not close over the environment in anotherfunc - h is undefined. func only closes over the global environment. This will work:

def anotherfunc(h):
    def func(): return h
    return func()

Because here, func is defined in anotherfunc, and in python 2.3 and greater (or some number like this) when they almost got closures correct (mutation still doesn't work), this means that it closes over anotherfunc's environment and can access variables inside of it. In Python 3.1+, mutation works too when using the nonlocal keyword.

Another important point - func will continue to close over anotherfunc's environment even when it's no longer being evaluated in anotherfunc. This code will also work:

def anotherfunc(h):
    def func(): return h
    return func

print anotherfunc(10)()

This will print 10.

This, as you notice, has nothing to do with lambdas - they are two different (although related) concepts.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .