Re: RFC: Anonymous Classes

From: Date: Mon, 23 Sep 2013 13:36:46 +0000
Subject: Re: RFC: Anonymous Classes
References: 1  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On 09/23/2013 07:39 AM, Joe Watkins wrote:
Morning All,
     https://wiki.php.net/rfc/anonymous_classes
     I'd like to hear thoughts regarding the addition of anonymous
classes, patch included. Cheers Joe
Thanks chaps ...
I am having a hard time picturing a real-world use-case for this. -Rasmus
I think since you wrote that quite a few decent use cases have been suggested, testing, MVC (that wasn't on internals), throw away/private implementation of interfaces or abstracts. It has such a wide range of uses, I didn't think it necessary to point them out, as others have mentioned it's an established OO pattern to use objects of an anonymous class.
I'd rather go for a new BaseType syntax, though: $subject->attach(new SplObserver() { function update(SplSubject $s) {
    printf("Got update from: %s\n" $subject);
} );
Using class_name_reference in place of T_CLASS causes a conflict with existing syntax, which is the reason T_CLASS is accepted, additionally, using class is nicer shorthand for new stdClass {}. With that in mind, I haven't tried moving the arguments, this: (new class {definition} (arguments))->method() seems more natural/sensible than: (new class(arguments) {definition})->method() new class() just feels a bit wrong ...
Frankly, constructors are not part of any interface. Besides a constructor is totally useless in an anonymous class as it will never be called after the object is created.
The examples in the constructor were not meant as use cases but to show the syntax of use. If you are using an anonymous class to implement an interface then a constructor may not be required, however, removing the ability to declare a constructor is restrictive an unnecessary. Cheers Joe

Thread (55 messages)

« previous php.internals (#69269) next »