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

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

Reply via email to