Re: RFC: Anonymous Classes

From: Date: Thu, 26 Sep 2013 19:08:09 +0000
Subject: Re: RFC: Anonymous Classes
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On Thu, Sep 26, 2013 at 5:59 PM, Joe Watkins <[email protected]> wrote:
> Ok, I included just about all the information on use cases that is obvious
> or has been discussed, so I think we got use cases covered now, right ??
>
> See a good one yet ??
>
>
> Cheers
> Joe

Wondering about scoping. Yes, anonymous classes can have their own
constructor, but if we're adding syntactic sugar why not make it
possible to use from the scope they're created in?

See this example adapted from a Stack Overflow question about how it
works in Java [1]:

(not sure how you the "use" syntax should look like)

public function doStuff($userId) {
    $this->doOtherStuff($userId, new class implements SomeInterface {
        public function onSuccess() {
             echo $userId;
        }
    });
}

vs

public function doStuff($userId) {
    $this->doOtherStuff($userId, new class implements SomeInterface {
        private $userId;
        public function __construct($userId) {
            $this->userId = $userId;
        }
        public function onSuccess() {
             echo $this->userId;
        }
    });
}

[1] http://stackoverflow.com/questions/3251018/java-anonymous-inner-class-using-a-local-variable

Cheers,

Alexander


Thread (55 messages)

« previous php.internals (#69357) next »