Re: [RFC] Anonymous Classes
| From: | Matthew Leverton | Date: | Tue, 24 Feb 2015 16:21:35 +0000 |
| Subject: | Re: [RFC] Anonymous Classes | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-83675@lists.php.net to get a copy of this message | ||
On Tue, Feb 24, 2015 at 7:52 AM, Philip Sturgeon <pjsturgeon@gmail.com> wrote:
> Good day!
>
> https://wiki.php.net/rfc/anonymous_classes
>
> There's a little RFC + patch that Joe Watkins put together, and as
> before with the ArrayOf RFC, I'll be helping out.
>
> So, lets get this discussion rolling.
>
Anonymous classes open up slightly new ways to do things. I like to
write a lot of small classes for the purposes of DI and re-usability.
Because of that, the code often looks like:
$foo = new Foo($depend1, $depend2, function() {
return 'my custom behavior';
});
That's fine when there's only one callback needed, but beyond that it
breaks down. This becomes more readable:
abstract class Foo
{
...
}
$foo = new class($depend1, $depend2) extends Foo {
public function doFoo() {
}
public function doBar() {
}
}
These cases for me are always single-use for the scope of the
application, and often it's inside configuration blocks that are
custom per environment. I have no need to name the class other than
PHP doesn't support anonymous ones.
I'm +1 on the concept, depending on what the maintenance impact that
this would bring to PHP's core. There are enough other ways to
accomplish the same goals that I don't think this is "must have", even
though I'd definitely use it.
Regarding this syntax (from other discussions on the list):
$foo = class { };
I think if such a thing were supported, maybe $foo would just be a
ReflectionClass object. (I haven't really thought that one through...)
--
Matthew Leverton