Re: [PATCH] [RFC] Closures and lambda functions in PHP

From: Date: Tue, 17 Jun 2008 12:24:01 +0000
Subject: Re: [PATCH] [RFC] Closures and lambda functions in PHP
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-38324@lists.php.net to get a copy of this message
Hi Marcus, I now have revised my patch to include your suggestions: http://www.christian-seiler.de/temp/closures-php-5.3-2008-06-17-2.diff The changes to the previous version: - \0 at the start of the compiled lambda function name is dropped. - lambdas which are class members are now marked as final - the generated name of the lambda is now also stored within the op_array (before op_array->function_name was simply "lambda") - your suggestions for code cleanups in zend_compile.c
Actually how does it integrate with reflection?
Consider the following class: class Example { private $x = 0; public function getIncer () {
    return function () {
      $this->x++;
    };
} public function show () {
    $this->reallyShow ();
} protected function reallyShow () {
    echo "{$this->x}\n";
} } Running Reflection::export(new ReflectionClass('Example')); will yield (among other things): - Methods [4] {
    Method [ <user> public method getIncer ] {
      @@ /home/christian/dev/php5.3/c-tests/reflection.php 6 - 10
    }
    Method [ <user> final public method __compiled_lambda_/home/christian/dev/php5.3/c-tests/reflection.php_0 ] {
      @@ /home/christian/dev/php5.3/c-tests/reflection.php 7 - 9
    }
    Method [ <user> public method show ] {
      @@ /home/christian/dev/php5.3/c-tests/reflection.php 12 - 14
    }
    Method [ <user> protected method reallyShow ] {
      @@ /home/christian/dev/php5.3/c-tests/reflection.php 16 - 18
    }
} So lambda functions appear simply as additional methods in the class, with their generated name. Of course, the ReflectionMethod / ReflectionFunction classes could be extended so that it provides and additional method isLambda() in order to determine whether a function actually is a lambda function. But I'd rather do that in a separate step and a separate patch. Regards, Christian

« previous php.internals (#38324) next »