Welcome Guest, Not a member yet? Register   Sign In
Use of Closures in Views
#17

Eventually, I hope to get around to replying to everything directed towards me since my last post. However, I wanted to point out that quirk #3 is of course isolated to 5.3, and does not exist in 5.4+. I also happen to know that @kilishan has used $this in an anonymous function at some point, because it had to be rewritten to support PHP 5.3 Smile

From the link about anonymous functions vs. closures, though:
Quote:From this we can derive: All Anonymous Functions are Closures, but not all Closures are Anonymous Functions.

Also:
Quote:Anonymous Functions are implemented as Closure objects. PHP takes the idea that “Anonymous Functions are Closures without a name” to heart, because that is EXACTLY what they are.

Finally:
Quote:How should you know when to call it a Closure or Anonymous Function? Simple: You can always call it a Closure (because all Anonymous Functions are Closures), and if it doesn’t have a name, it is Anonymous. Pretty simple, right?

However, I don't think this is necessarily correct, it's just an artifact of PHP's implementation. The fact that PHP implements an anonymous function as a Closure object doesn't really make it a closure. This just tells you that PHP allows you to create Closures which are anonymous functions, and Closures which are not anonymous functions, except that I'm not sure whether you could actually create a class which extends Closure to create a "named" Closure.

In the long run, I think you'll find that PHP's implementation of Closure is not quite what most people expect from a closure when coming from another language. As my later example demonstrates, the Closure or anonymous function is not isolated from the executing scope, even if I were to pass variables from the defining scope with the use() construct. Of course, the fact that I used a class from the executing scope within my Closure also means I posted an example which I would consider bad code.

I'd also like to point out that, although it's helpful in some circumstances, comparing JavaScript to PHP will eventually give you a headache. JavaScript's entire idea of what a function, object, or variable is happens to be intertwined in a way that makes comparison to languages with a more traditional use of these concepts difficult, at best. This is also why there are a whole slew of meta-languages which compile to JavaScript but enforce the more traditional concepts.
Reply


Messages In This Thread
Use of Closures in Views - by mwhitney - 09-09-2015, 09:25 AM
RE: Use of Closures in Views - by includebeer - 09-12-2015, 10:25 AM
RE: Use of Closures in Views - by mwhitney - 09-14-2015, 10:15 AM
RE: Use of Closures in Views - by PaulD - 09-15-2015, 03:37 PM
RE: Use of Closures in Views - by kilishan - 09-16-2015, 06:31 AM
RE: Use of Closures in Views - by jLinux - 09-21-2015, 03:28 PM
RE: Use of Closures in Views - by spjonez - 09-24-2015, 06:57 AM
RE: Use of Closures in Views - by mwhitney - 09-24-2015, 07:29 AM
RE: Use of Closures in Views - by kilishan - 09-24-2015, 07:20 AM
RE: Use of Closures in Views - by spjonez - 09-24-2015, 03:42 PM
RE: Use of Closures in Views - by mwhitney - 09-25-2015, 10:03 AM
RE: Use of Closures in Views - by jLinux - 09-25-2015, 06:46 AM
RE: Use of Closures in Views - by mwhitney - 09-25-2015, 10:29 AM
RE: Use of Closures in Views - by kilishan - 09-25-2015, 07:04 AM
RE: Use of Closures in Views - by jLinux - 09-25-2015, 07:12 AM
RE: Use of Closures in Views - by jLinux - 09-25-2015, 07:17 AM
RE: Use of Closures in Views - by kilishan - 09-25-2015, 07:32 AM
RE: Use of Closures in Views - by jLinux - 09-25-2015, 08:13 AM
RE: Use of Closures in Views - by mwhitney - 09-25-2015, 08:26 AM
RE: Use of Closures in Views - by spjonez - 09-25-2015, 11:00 AM
RE: Use of Closures in Views - by mwhitney - 09-28-2015, 10:51 AM
RE: Use of Closures in Views - by kilishan - 09-25-2015, 01:24 PM



Theme © iAndrew 2016 - Forum software by © MyBB