Re: [RFC] Allow "use( $longname as $alias)" syntax for closures declaration
| From: | Laruence | Date: | Thu, 12 Apr 2012 16:25:58 +0000 |
| Subject: | Re: [RFC] Allow "use( $longname as $alias)" syntax for closures declaration | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-59795@lists.php.net to get a copy of this message | ||
On Fri, Apr 13, 2012 at 12:21 AM, Nikita Popov
<nikita.ppv@googlemail.com> wrote:
> On Thu, Apr 12, 2012 at 6:06 PM, Laruence <laruence@php.net> wrote:
>> On Fri, Apr 13, 2012 at 12:04 AM, Dmitri Snytkine
>> <dsnytkine@ultralogistics.com> wrote:
>>> I'm curious to know the benefits of this.
>>> Care to explain why or when this would be useful?
>>
>> From what LawnGnome said:
>>
>> " I think it's a nice bit of syntactic sugar, personally. It's not
>> something you'd ever _need_, but it's a neat way of getting your
>> variable names in order within the inner scope if your enclosing scope
>> is a bit messy. Plus, it's consistent with the X as Y syntax that's
>> already in foreach and traits, which is a bonus."
>
> It would be nice to see a few real-life scenarios where this is
> useful. Right now I can't think of situations where you'd want to
> change the variable name between the outer scope and the closure
> scope. Wouldn't that just be confusion for the programmer if the same
> variable would go under two different names?
Now you can not use $this
class foo {
public function bar() {
return function() use ($this) { // PHP Fatal error:
Cannot use $this as lexical variable
}
}
}
so you have to write:
class foo {
public function bar() {
$obj = $this;
return function() use ($obj) {
}
}
}
thanks
>
> Nikita
--
Laruence Xinchen Hui
http://www.laruence.com/