On Sat, May 6, 2023, at 2:16 AM, Rokas Šleinius wrote:
> On Fri, 5 May 2023 at 23:58, Levi Morrison via internals
> <internals@lists.php.net> wrote:
>>
>> I am not sure about making them final by default. I think it's a more
>> consistent language design to allow `new final class()` as you
>> originally proposed. Although I don't know why anyone would want to
>> extend anonymous classes, we can see that people do, in fact, do it. I
>> don't see why we couldn't allow `new class() extends $someClass` in
>> the future to specifically allow them to do this. I mean, I'm not
>> going to lobby for it, I'm just pointing out that is more aligned and
>> consistent with other parts of the language, than to simply make it
>> final by default.
>>
>> Cheers.
>>
>> --
>> PHP Internals - PHP Runtime Development Mailing List
>> To unsubscribe, visit: https://www.php.net/unsub.php
>>
>
> My work currently is integrating with various shipping provider APIs
> such as DHL. I have to deal with the enormous API request (and
> response) definitions. I am just creating a DTO class for each level
> of JSON key, which results in a HUGE amount of classes.
>
> I have a hunch this problem can be solved better with anonymous
> classes, but their support is not great yet - PHPStorm has a breaking
> bug with their autocompletion and I am too dumb to figure out a better
> solution in any case.
I think you're on the right track with named classes, honestly. With current PHP versions that's syntactically a lot nicer than it used to be.
> However, I totally agree with Levi in that the way forward is better
> towards `new class() extends $someClass` and not "all anonymous
> classes final by default". It may open patterns and opportunities we
> have yet to think about.
My understanding of the engine implementation is that extending from a class dynamically would be really really hard. (Anon classes are not defined at runtime, they get defined at compile time, just with an auto-generated name.) That said, I have to agree here as well that I'm not a fan of forcing anon classes to be final, but if someone could figure out dynamic-extension anon classes, I would be a happy camper.
--Larry Garfield