On 6/17/08, Larry Garfield <[EMAIL PROTECTED]> 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/

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to