Re: [PHP] Closure and $this
Many thanks Larry. I like the Closure::bind() method proposal. Best regards __ Raymond Do more with less - http://raxanpdi.com --- On Thu, 1/13/11, Larry Garfield la...@garfieldtech.com wrote: From: Larry Garfield la...@garfieldtech.com Subject: Re: [PHP] Closure and $this To: php-general@lists.php.net Date: Thursday, January 13, 2011, 1:57 AM On Wednesday, January 12, 2011 11:37:19 pm Greg Bair wrote: On Wed, 12 Jan 2011 20:02:11 -0800 (PST) Raymond Irving xwis...@yahoo.com wrote: Hello, Does anyone know if closures will ever support the $this keyword? I think it would be very useful when working with objects. Best regards__RaymondDo more with less - http://raxanpdi.com Probably not, and my understanding of why comes from this line from the docs (http://www.php.net/manual/en/functions.anonymous.php): Anonymous functions are currently implemented using the Closure class. So, in other words, your closure does not belong to the class you declare it in, but the Closure class. Thus, if it supported the $this variable, it would refer not to the class you want, but instead to the Closure class. Just my understanding. If it's not right, someone point it out. Actually at one point early on they did support a $this, but the way it was bound to an object was half-assed and incomplete so it was removed entirely from 5.3. The intent was to then properly think through how that binding should happen and re-introduce it properly in 5.4. I believe a consensus was reached on how that should happen but I'm not sure what its implementation status is at present. I believe this is the relevant RFC: http://wiki.php.net/rfc/closures/object-extension --Larry Garfield -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Closure and $this
On Wed, Jan 12, 2011 at 10:57 PM, Larry Garfield la...@garfieldtech.comwrote: I believe this is the relevant RFC: http://wiki.php.net/rfc/closures/object-extension That was a good bedtime read last night, Larry. I prefer method A which is nearly identical to Java's inner classes where $this would remain tied to whatever it held when the closure was created and gain the object's scope. I do like the idea of being able to explicitly bind the closure to a new object as well. That's all well and good come PHP 5.4 or 6.0, but for now you are limited to holding a reference to $this without gaining its scope, i.e. you can only access public members. Also, you must assign $this to a new variable because you cannot pass $this in the use() clause. $that = $this; $closure = function(...) use ($that) { ... $that-property ... $that-method() ... } If you need access to a protected or private variable inside the closure, you can pass a reference to it inside use(). Once again you need to first assign the reference to a new variable and then pass that in to the closure. Important: you must use the operator in the use() clause as well as when creating the reference. class ClosureFactory { private $count = 0; public function create() { $ref = $this-count; return function() use ($ref) { return ++$ref; }; } } $factory = new ClosureFactory(); $closure = $factory-create(); for ($i = 0; $i 5; $i++) { echo $closure(); } Yields 12345 David
Re: [PHP] Closure and $this
On Wed, 12 Jan 2011 20:02:11 -0800 (PST) Raymond Irving xwis...@yahoo.com wrote: Hello, Does anyone know if closures will ever support the $this keyword? I think it would be very useful when working with objects. Best regards__RaymondDo more with less - http://raxanpdi.com Probably not, and my understanding of why comes from this line from the docs (http://www.php.net/manual/en/functions.anonymous.php): Anonymous functions are currently implemented using the Closure class. So, in other words, your closure does not belong to the class you declare it in, but the Closure class. Thus, if it supported the $this variable, it would refer not to the class you want, but instead to the Closure class. Just my understanding. If it's not right, someone point it out. -- Greg Bair PHP Developer -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Closure and $this
On Wednesday, January 12, 2011 11:37:19 pm Greg Bair wrote: On Wed, 12 Jan 2011 20:02:11 -0800 (PST) Raymond Irving xwis...@yahoo.com wrote: Hello, Does anyone know if closures will ever support the $this keyword? I think it would be very useful when working with objects. Best regards__RaymondDo more with less - http://raxanpdi.com Probably not, and my understanding of why comes from this line from the docs (http://www.php.net/manual/en/functions.anonymous.php): Anonymous functions are currently implemented using the Closure class. So, in other words, your closure does not belong to the class you declare it in, but the Closure class. Thus, if it supported the $this variable, it would refer not to the class you want, but instead to the Closure class. Just my understanding. If it's not right, someone point it out. Actually at one point early on they did support a $this, but the way it was bound to an object was half-assed and incomplete so it was removed entirely from 5.3. The intent was to then properly think through how that binding should happen and re-introduce it properly in 5.4. I believe a consensus was reached on how that should happen but I'm not sure what its implementation status is at present. I believe this is the relevant RFC: http://wiki.php.net/rfc/closures/object-extension --Larry Garfield -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php