Re: [RFC] Allow "use( $longname as $alias)" syntax for closures declaration
| From: | Anthony Ferrara | Date: | Thu, 12 Apr 2012 17:09:15 +0000 |
| Subject: | Re: [RFC] Allow "use( $longname as $alias)" syntax for closures declaration | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-59797@lists.php.net to get a copy of this message | ||
The only real use that I can see would be that it allows:
public function getCallback() {
return function() use ($this as $that) {
$that->doSomething();
}
}
However, I think that would be confusing, since $that would only have
public access...
I'm not against the idea, I just don't see a strong use-case for it.
It's not like your injecting the scoped vars into an existing
function, you're writing the function fresh. So why not just use the
existing var names?
Anthony
On Thu, Apr 12, 2012 at 12:36 PM, Ralph Schindler
<ralph@ralphschindler.com> wrote:
> On 4/12/12 11:25 AM, Laruence 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
>> }
>> }
>> }
>>
>> so you have to write:
>>
>> class foo {
>> public function bar() {
>> $obj = $this;
>> return function() use ($obj) {
>> }
>> }
>> }
>>
>> thanks
>
>
> Correct me if I am wrong, but this is possible in 5.4 with the
> Closure::bindTo() API, as well as the implied binding of $this.
>
> Working in my code:
>
>
> class ServiceLocator
> {
> protected $services = array();
>
> public function __construct()
> {
> $this->services['DbAdapter'] = function () {
> return new DbAdapter(
> $this->get('Configuration')['db']
> );
> };
> $this->services['UserTable'] = function () {
> return new TableGateway(
> 'user',
> $this->get('DbAdapter')
> );
> };
> }
> public function get($name) {
>
> ...
>
>
> -ralph
>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>