Re: RFC: Anonymous Classes
| From: | Joe Watkins | 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 internals+get-69269@lists.php.net to get a copy of this message | ||
On 09/23/2013 07:39 AM, Joe Watkins wrote:
Morning All,Thanks chaps ...https://wiki.php.net/rfc/anonymous_classesI'd like to hear thoughts regarding the addition of anonymousclasses, patch included. Cheers Joe
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 am having a hard time picturing a real-world use-case for this. -Rasmus
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 ...I'd rather go for anew BaseTypesyntax, though: $subject->attach(new SplObserver() { function update(SplSubject $s) {printf("Got update from: %s\n" $subject);} );
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 JoeFrankly, 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.