[PSR-12] Anonymous classes

262 views
Skip to first unread message

Alessandro Lai

unread,
Jan 24, 2020, 3:46:13 AM1/24/20
to PHP Framework Interoperability Group
While following PSR-12 implementation on PHP-CS-Fixer, a comment indicated a possible inconsistency in the PSR-12 text: https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/4502#issuecomment-578006523

The issue is in the fact that chapter 4 says:

The term “class” refers to all classes, interfaces, and traits.
[...]
When instantiating a new class, parentheses MUST always be present even when there are no arguments passed to the constructor.

But chapter 8 for anonymous classes says:

Anonymous Classes MUST follow the same guidelines and principles as closures in the above section.

<?php

$instance = new class {};


Should we make this clearer?

Alex Makarov

unread,
Jan 24, 2020, 5:12:59 AM1/24/20
to php...@googlegroups.com
How would you word it instead?
> --
> You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/4f9352d1-f65b-47bb-9c08-e06ecc6ece1f%40googlegroups.com.

Alessandro Lai

unread,
Jan 24, 2020, 6:23:06 AM1/24/20
to PHP Framework Interoperability Group
First we need the opinion of the editor (Korvin Szanto), to decide if we want to correct course or just clarify our intention.

IMHO, looking at the code example, it seems that we're not requiring the parentheses, but it would be inconsistent, so I would prefer an errata that requires them.

This PR contains my proposed fix, minus the errata text that should be added in the meta: https://github.com/php-fig/fig-standards/pull/1206

Il giorno venerdì 24 gennaio 2020 11:12:59 UTC+1, Alex Makarov ha scritto:
How would you word it instead?

On Fri, Jan 24, 2020 at 11:46 AM Alessandro Lai
<alessand...@gmail.com> wrote:
>
> While following PSR-12 implementation on PHP-CS-Fixer, a comment indicated a possible inconsistency in the PSR-12 text: https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/4502#issuecomment-578006523
>
> The issue is in the fact that chapter 4 says:
>
> The term “class” refers to all classes, interfaces, and traits.
> [...]
> When instantiating a new class, parentheses MUST always be present even when there are no arguments passed to the constructor.
>
> But chapter 8 for anonymous classes says:
>
> Anonymous Classes MUST follow the same guidelines and principles as closures in the above section.
>
> <?php
>
> $instance = new class {};
>
>
> Should we make this clearer?
>
> --
> You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+unsubscribe@googlegroups.com.

Alex Makarov

unread,
Jan 24, 2020, 6:35:55 AM1/24/20
to php...@googlegroups.com
I'd prefer errata as well.
>> > To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
> --
> You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/53ab5bcd-6f18-4b71-aee3-1f1838a98d84%40googlegroups.com.

Korvin Szanto

unread,
Jan 24, 2020, 12:13:52 PM1/24/20
to PHP Framework Interoperability Group
I'd argue what is confusing here is the lack of parenthesis on the anonymous class in the example and not that section 4 doesn't cover anonymous classes explicitly. If we do anything, it would be that we add adjust the code sample and describe the change in errata.


On Friday, January 24, 2020 at 3:35:55 AM UTC-8, Alex Makarov wrote:
I'd prefer errata as well.

On Fri, Jan 24, 2020 at 2:23 PM Alessandro Lai
<alessan...@gmail.com> wrote:
>
> First we need the opinion of the editor (Korvin Szanto), to decide if we want to correct course or just clarify our intention.
>
> IMHO, looking at the code example, it seems that we're not requiring the parentheses, but it would be inconsistent, so I would prefer an errata that requires them.
>
> This PR contains my proposed fix, minus the errata text that should be added in the meta: https://github.com/php-fig/fig-standards/pull/1206
>
> Il giorno venerdì 24 gennaio 2020 11:12:59 UTC+1, Alex Makarov ha scritto:
>>
>> How would you word it instead?
>>
>> On Fri, Jan 24, 2020 at 11:46 AM Alessandro Lai
>> <alessan...@gmail.com> wrote:
>> >
>> > While following PSR-12 implementation on PHP-CS-Fixer, a comment indicated a possible inconsistency in the PSR-12 text: https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/4502#issuecomment-578006523
>> >
>> > The issue is in the fact that chapter 4 says:
>> >
>> > The term “class” refers to all classes, interfaces, and traits.
>> > [...]
>> > When instantiating a new class, parentheses MUST always be present even when there are no arguments passed to the constructor.
>> >
>> > But chapter 8 for anonymous classes says:
>> >
>> > Anonymous Classes MUST follow the same guidelines and principles as closures in the above section.
>> >
>> > <?php
>> >
>> > $instance = new class {};
>> >
>> >
>> > Should we make this clearer?
>> >
>> > --
>> > You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
>> > To unsubscribe from this group and stop receiving emails from it, send an email to php...@googlegroups.com.
>> > To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/4f9352d1-f65b-47bb-9c08-e06ecc6ece1f%40googlegroups.com.
>
> --
> You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to php...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages