On Tuesday 17 June 2008, Christian Seiler wrote:
> Hi,
>
> > - I am a little confused about the OOP interaction.  How does a function
> > become a public method of the class?
>
> To clarify: the "public method" ist just the internal representation of
> the lambda function and has *nothing* to do with the semantics of
> calling the lambda itself. The "method" only means that the lambda
> function defined inside another method can access the class members and
> "public" only means that the lambda function can still be called from
> outside the class.

If one knew how to access it, which it seems is not possible/feasible for 
user-space code.  

> > 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);
>
> No, that's not what my patch does. My patch does:
>
> class Example {
>    private $a = 2;
>
>    public function myMethod ($b) {
>      return function () {
>        lexical $b;
>        return $this->a * $b;
>      };
>    }
> }
>
> $e = new Example ();
> $lambda = $e->myMethod (4);
> var_dump ($lambda ()); // int(8)
> $lambda2 = $e->myMethod (6);
> var_dump ($lambda2 ()); // int(12)
>
> So esentially, it does not matter whether you define a lambda function
> inside a method or a function (or in global scope, for that matter), you
> always use it the same way. The in-class-method lambda function only has
> the additional advantage of being able to access the private and
> protected class members since *internally* it is treated like a public
> class method.

I see.  It would be great if you could update the RFC with this information so 
that it's clearer.  

> If you want to add methods dynamically to classes, why not use the
> runkit extension? I really don't see a point in making lambda functions
> and closures something they are not.

I was asking if they could be used for that, not to make them into a different 
animal.  As for using runkit, do I really need to answer that? :-)

Two other questions that just occurred to me:

1) What is the interaction with namespaces, if any?  Are lambdas as 
implemented here ignorant of namespace, or do they take the namespace where 
they are lexically defined?

2) What happens with the following code?

class Foo {
  private $a;
}

$f = new Foo();

$b = 5;

$f->myfunc = function($c) {
  lexical $b;
  print $a; // This generates an error, no?
  print $b; // This prints 5, right?
  print $c; // Should print whatever $c is.
}

$f->myfunc(3);

Or is the above a parse error entirely?

-- 
Larry Garfield                  AIM: LOLG42
[EMAIL PROTECTED]               ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson

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

Reply via email to