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

From: Date: Tue, 17 Jun 2008 04:58:58 +0000
Subject: Re: [PATCH] [RFC] Closures and lambda functions in PHP
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-38309@lists.php.net to get a copy of this message
On 6/17/08, Larry Garfield <larry@garfieldtech.com> wrote: > - I am a little confused about the OOP interaction. How does a function > become a public method of the class? > > class Example { > private $a = 2; > > function myMethod($b) { > $lambda = function() { > lexical $b; > return $this->a * $b; // This part I get > }; > return $lambda; > } > } > > $e = new Example(); > $lambda = $e->myMethod(); > $e->$lambda(5); > > That doesn't seem right at all, but that's how I interpret "Essentially, > > closures inside methods are added as public methods to the class that > > contains the original method." Can you give an example of what that actually > means? As far as I understand, it means following: class Example { private $a = 1; private function b() { return 2; } public function getLambda($param) { $lambda = function($lparam) { lexical $param; return $this->a + $this->b() + $param + $lparam; } return $lambda; } } $obj = new Example(); $lambda = $obj->getLambda(3); $result = $lambda(4); // 1+2+3+4 => 10 -- Alexey Zakhlestin http://blog.milkfarmsoft.com/

« previous php.internals (#38309) next »