-3
  • Are closures possible in PHP 5.3?
  • I'm looking for a simple closure example, minimal code is appreciated.
  • If closures are possible in PHP 5.3, does it behave similarly to Javascript's closures in terms of scoping?
  • What are the difference between Javascript closures and PHP closures (if it exists)?
Steve Nguyen
  • 5,854
  • 5
  • 21
  • 39
  • 1
    I think you should read the documentation because it's really helpful. http://php.net/manual/en/functions.anonymous.php Have you already read? – Antonio Laguna Jul 13 '11 at 13:11
  • 1
    @FinalForm: Closures are anonymous functions with external functions, and anonymous functions are closures, with `0` external references respectively. I don't know, if the "writer is a retard", but the statement is not completely wrong (even if its not completely right). – KingCrunch Jul 13 '11 at 13:22
  • 1
    @FinalForm: That doesn't mean you can't read the rest of that page. You would have realized within seconds that it's what you're looking for "Example 3: Closures and scoping" – phant0m Jul 13 '11 at 13:25
  • 1
    @KingCrunch Closures are the combination of nested function inside an outer function, involving a memory stack that persists after the outer function returns, a binding of an environment with a function that acts within its own environment. An anonymous function is a function that is executed immediately within a program flow that lacks a name, therefore it is anonymous. So closures and anonymous functions are not the same thing. An anonymous function "CAN" be a closure but NOT IS a closure. – Steve Nguyen Jul 13 '11 at 13:29
  • 2
    @FinalForm: Wow, I don't know, where to start... For example: An anonymous is usually not executed immediately, because in this case nobody needs a (anonymous) function, but one can put the code directly into place. Anonymous functions are just functions usually referenced by a variable without identifier. The only difference to a closure is "a binding of an environment", that you mention yourself, but now its just a point of view, if an anon-function is a closure without any binding, our not. However, if you know, what a closure is, why you ask this question? – KingCrunch Jul 13 '11 at 13:33
  • @KingCrunch I'm asking for the existence of closures in PHP and it's similarities/differences to closures in Javascript. The question is **NOT** "what is a closure?" – Steve Nguyen Jul 13 '11 at 13:35
  • The existence of closures remkains a question best answered in the documentation and/or via Google. Or even in other, already-existing answers right here on Stack. – Winfield Trail Jul 13 '11 at 13:43
  • @FinalForm occasionally mobile copy and paste betrays me. link fixed. – Winfield Trail Jul 13 '11 at 13:51
  • @Kerin Dude, that is not the same question as mine. I'm asking for a comparison between closures in Javascript and PHP. It's clearly stated above in my post. – Steve Nguyen Jul 13 '11 at 13:51
  • @FinalFight, argh. Sometimes I really hate touch screens - the final, actual link is here. http://stackoverflow.com/questions/1989352/are-php-closures-broken-or-am-i-missing-something – Winfield Trail Jul 13 '11 at 15:08
  • 1
    While I apologize for the bad links, you would have found your answer if you had actually searched. – Winfield Trail Jul 13 '11 at 15:09
  • Also take a look at http://php.net/manual/en/functions.anonymous.php – matthiasmullie Jul 13 '11 at 13:13

1 Answers1

7
  • Are closures possible in PHP 5.3?

Yes

  • I'm looking for a simple closure example, minimal code is appreciated.
$text = "Hello World";
$closure = function () use ($text) { echo $text; }
$closure();
  • If closures are possible in PHP 5.3, does it behave similarly to Javascript's closures in terms of scoping?
  • What are the difference between Javascript closures and PHP closures (if it exists)?

Don't know. I never had a deeper look at JSs closures, but maybe you get an idea of whats PHPs closures are like after reading Wikipedia: Closure (Computer Science)

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • A note about scoping: Closures behave just like any function/method in PHP where they incur the local scope; unlike javascript which can access 'global' vars like normal. To get around this, closures use the `use($var)` statement to get vars from outside local scope. You can also do `use(&$var)` to modify the var outside and `use($var, $params)` for multiple. – adlawson Jul 13 '11 at 13:17
  • You can access global vars in PHPs closures too, but in PHP using global vars are discouraged anyway, independent from closures, functions or methods. The `use()`-statement is that, what differs closures from simple anonymous functions: It creates the reference to non-local variables (see wikipedia article). – KingCrunch Jul 13 '11 at 13:19
  • by declaring `global $var;` before you use it, yeah; but when you use closures inside class methods, using `global` doesn't work as expected (well it does, it just goes to the root, not up a level). – adlawson Jul 13 '11 at 13:22
  • @adlawson @KingCrunch I'm having issues finding use() on PHP.net, can you link me? – Steve Nguyen Jul 13 '11 at 13:23
  • @adlawson: Of course `globals` means "global" and not just "this and all children", "up a level", or something. However, globals are not recommended anyway ;) Thus I don't know, why we are talking about them: They behave like usual and nothing else. @FinalForm: http://php.net/manual/en/functions.anonymous.php (its already mentioned in the comments of your question) – KingCrunch Jul 13 '11 at 13:24
  • @kingcrunch That was my point exactly. Use `use($var)` for closures. Never use `global $var` anywhere. – adlawson Jul 13 '11 at 13:33
  • @FinalForm: That's because it's not a function but a language construct such as `if()` – phant0m Jul 13 '11 at 13:41