Re: Revisiting Anonymous Classes

From: Date: Mon, 30 Jun 2014 06:45:53 +0000
Subject: Re: Revisiting Anonymous Classes
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-75135@lists.php.net to get a copy of this message
Hi! > I would like to propose the following syntax for declaring an > anonymous class that does not implement an interface or extend > a class: What would be the use case for such class? I can understand what is the use case for a just-in-time class that implement certain interface or extends certain class - mocks, stubs, handlers, etc. - e.g. widely used in Java in this way. However free-standing anonymous class, not connected to any hierarchy, does not seem to be very useful - why not have a real class then, or if it's too much trouble, an anon function? I know C# has them, but in that case it plays more of a role that stdclass or just array plays in PHP. OTOH, if you just make your anon class extend stdclass you have the same effect but without a special case that looks a bit weird to me. But maybe it's not a real problem since again you can do the same with stdclass. > $o = new AnInterface { Looks very similar to what Java does except Java does () and allows to pass ctor arguments. Are we assuming here anonymous classes do not have ctors, never call them or something else? A bit of bikeshedding: if we don't want to follow Java here, we could also do this: $o = class { public function method() { ... } ... } as AnInterface; This is (partially) stolen from Groovy syntax and follows roughly the same model as anon functions (definition without the name). Also, this is a bit like you define anonymous struct type in C and C-like languages. -- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/

« previous php.internals (#75135) next »