Re: RFC: Anonymous Classes

From: Date: Tue, 24 Sep 2013 11:23:29 +0000
Subject: Re: RFC: Anonymous Classes
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-69311@lists.php.net to get a copy of this message
Lazare Inepologlou <linepogl@gmail.com> schrieb: > I use anonymous classes very frequently in Java and in C#, and I would say > that they are quite useful. However the examples given in the RFC are > really bad ones. Why on earth would you need a constructor for an anonymous > class? Anonymous classes are used to implement quickly some interface. > 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 need for anonymous classes in Java mostly comes from implementing interfaces on demand due to the lack of lamdas and the requirements of a strongly typed language. PHP has lambdas and it doesn't require you to match typing. Therefore I think the preferred way for most of the use cases is to use a lambda or if a bigger implementation is necessary, to give it a name. Due to namespacing you don't pollute the global scope anyway. Therefore I don't see much reason left as to why add anonymous classes and I really have a hard time finding a use case, except for trying to write PHP as you would write Java (which most likely is a bad idea). - David

« previous php.internals (#69311) next »