rfc:useas

Request for Comments: Allow use T_AS in closure use statement

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
rfc/useas.txt · Last modified: 2013/05/28 08:17 by tyrael