Re: [RFC] Allow "use( $longname as $alias)" syntax for closures declaration

From: Date: Thu, 12 Apr 2012 17:34:31 +0000
Subject: Re: [RFC] Allow "use( $longname as $alias)" syntax for closures declaration
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-59798@lists.php.net to get a copy of this message
On Thu, Apr 12, 2012 at 6:25 PM, Laruence <laruence@php.net> wrote: > Now you can not use $this > >      class foo { >           public function bar() { >               return function() use ($this) {  // PHP Fatal error: > Cannot use $this as lexical variable >               } >           } >      } And it is good that you can't - as of PHP 5.4 the $this variable is automatically bound, unless the closure is declared static. Adding a possibility to alias $this could get really messy imho. With the auto-binding the behavior is clear, but with the $this as $that syntax several questions open up: a) Is $this the object as the public sees it? I.e. the same as with the manual $that = $this assignment. Or does it also expose private properties? b) If you use &$this as $that, will $that automatically be rebound to the class it is used in? Nikita

« previous php.internals (#59798) next »