Allow use T_AS in closure use statement View as Markdown

Withdrawn
Authors
Xinchen Hui laruence@php.net
Date
2012/04/12
Version
1.0
Source
wiki.php.net/rfc/useas

Discussions on the mailing list

Call for voting: [RFC] Allow use T_AS in closure use statement
by Laruence · 14 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:53 UTC

Introduction

Allow use AS in the closure declaration, like:

<?php
  $foo = "bar";
  
  $func = function () use ($foo as $dup, &$foo as $ref) { //alias name
  };
  

Proposal

I have made a patch, after patch, the following script:

<?php

$a = "2";

$b = function() use (&$a as $b, $a as $c, $a as $e) {
    echo $b;
    echo $c;
    echo $e;
    $b = 4;
};

$b();

echo $a;

class Foo {
    public function closure() {
        return function () use ($this as $obj) {
            var_dump($obj);
        };
    }
}

$foo = new Foo();

$a = $foo->closure();
$a();

will output:

2224object(Foo)#2 (0) {
}

I will be keeping improve this patch.

Patch

Changelog

  • 2012-04-12 Xinchen Hui Initial version
  • 2012-07-18 Xinchen Hui Open voting
  • 2012-07-19 Xinchen Hui Draw back, add dynamic variable supporting