per-coding-style icon indicating copy to clipboard operation
per-coding-style copied to clipboard

New PHP 8.4 Syntax: new MyClass()->method() without parentheses

Open KorvinSzanto opened this issue 1 year ago • 6 comments

new Foo()->baz() will be allowed in PHP 8.4, we should cover it in the next version of PER-CS.

To me this is similar to the question of new Foo vs new Foo() where we decided the latter was better for readability: https://github.com/php-fig/per-coding-style/blob/master/spec.md#4-classes-properties-and-methods

KorvinSzanto avatar Jul 12 '24 21:07 KorvinSzanto

What should we say here? "Favor using the non-parens version if you can", or is there more to say on the matter?

Crell avatar Jul 13 '24 13:07 Crell

I'm of the opinion that it's simpler to see at a glance that a new instantiation is happening with wrapping parenthesis, but I don't think it's so much of an improvement that it justifies adding the wrapper in every situation.

Given that this is technically existing syntax, we'd need to go to 3.0 (or drop the thinking in #10) in order to add MUST NOT use parenthesis though we could require the opposite opposite since that's backwards compatible. In 2.1 we could add something like:

When chaining new instantiations with method calls (IE new Foo()->baz(), the instantiation - including the new keyword - SHOULD NOT be wrapped in parenthesis. When dealing with a complex statement, wrapping parenthesis MAY be used for readability.

KorvinSzanto avatar Oct 07 '24 17:10 KorvinSzanto

As usual, I don't think "never use this fancy new syntax that was just approved" is going to fly. The RFC passed 25 to 4. :-) So let's take "MUST NOT" use it off the table, as it's clear people want to use it.

The question is whether we encourage it, or stay silent. I'm leaning toward silent at the moment, but could be convinced otherwise. (We can always say something about it in a future version.)

Crell avatar Oct 07 '24 23:10 Crell

As usual, I don't think "never use this fancy new syntax that was just approved" is going to fly. The RFC passed 25 to 4. :-) So let's take "MUST NOT" use it off the table, as it's clear people want to use it.

I agree, but I think you're misinterpreting my comment. I said "MUST NOT use parenthesis" which would mean we advocate for the thing that passed 25 to 4.

I'm saying that we cannot advocate stronger than a SHOULD until 3.0 unless we advocate for the thing that requires no code change, which I agree we shouldn't do.

KorvinSzanto avatar Oct 07 '24 23:10 KorvinSzanto

Oh! I see what you mean. I'm open to a SHOULD here if the rest of the WG is.

Crell avatar Oct 07 '24 23:10 Crell

@Crell sounds more weighted than MUST for the case :+1:

samdark avatar Oct 08 '24 14:10 samdark

I don't think "you must use this new syntax in this way" counts as a BC break. It would only be a BC break if existing code suddenly becomes non-compliant with a new version.

Crell avatar Jan 17 '25 17:01 Crell