News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Davey Shafik's Blog:
The Closure Puzzle
January 16, 2012 @ 09:52:38

Davey Shafik has posted about an interesting find with closures in PHP revolving around an update to add "$this" access inside the closure.

However, it didn't stop there; there was also the addition of Closure::bind() and Closure->bindTo(). These methods are identical except one is a static method into which the closure is passed, the second an instance method on the closure itself. These methods both take two arguments (on top of the closure for the static version): $newthis and $newscope. What this means is that unlike the regular object model the concept of $this and lexical scope (what is in scope for the function with regards to private/protected methods inside objects) are completely separated.

He also mentions that you can change the "$this" to a different object (complex) or swapping out the object the closure is bound to while keeping "$this" the same (simpler). He mentions that it could be useful for unit testing but can have its drawbacks. He's included code to illustrate the breakage it can cause in the PHP OOP model (with an explanation).

0 comments voice your opinion now!
closure puzzle bindto bind oop object


blog comments powered by Disqus

Similar Posts

Kevin Schroeder's Blog: Passing typed ActionScript objects to Flex using PHP and a message queue

DevShed: The Basics of Serializing Objects in PHP

Brandon Savage: The "D" Doesn’t Stand For Dependency Injection

Stefan Mischook's Blog: Classes and Objects in PHP

Brandon Savage's Blog: 5 (Good) Lessons The Government Teaches Us About Object-Oriented Programming


Community Events





Don't see your event here?
Let us know!


list laravel introduction voicesoftheelephpant series deployment community bugfix release opinion interview framework language symfony tips install podcast library package api

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework