rfc:new_without_parentheses
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:new_without_parentheses [2024/04/07 01:19] – swapped Proposal and Ambiguity sections, improved text vudaltsov | rfc:new_without_parentheses [2024/05/28 18:04] (current) – Added Target Version vudaltsov | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: new MyClass()-> | ====== PHP RFC: new MyClass()-> | ||
- | * Date: 2024-04-07 | + | * Date: 2023-12-29 |
- | * Author: Valentin Udaltsov | + | * Author: Valentin Udaltsov |
- | * Status: | + | * Status: |
+ | * Target Version: PHP 8.4 | ||
+ | * Discussion: https:// | ||
* First Published at: http:// | * First Published at: http:// | ||
* Implementation: | * Implementation: | ||
Line 15: | Line 17: | ||
<code php> | <code php> | ||
- | class MyClass | + | class Request implements Psr\Http\Message\RequestInterface |
{ | { | ||
- | | + | |
- | { | + | |
- | echo ' | + | |
- | } | + | |
} | } | ||
- | (new MyClass())->method(); // Hello, World! | + | // OK |
+ | $request = (new Request())->withMethod(' | ||
- | new MyClass()-> | + | // PHP Parse error: syntax error, unexpected token " |
+ | $request = new Request()-> | ||
</ | </ | ||
The goal of this RFC is to enable the second syntax to: | The goal of this RFC is to enable the second syntax to: | ||
- | * make coding with PHP more convenient | + | * make coding with PHP more convenient |
- | * lower the learning curve | + | * decrease the visual debt in all sorts of builders and configurators ([[https:// |
- | * decrease the visual debt | + | |
* ease switching between other C-like languages that don't require parentheses ([[https:// | * ease switching between other C-like languages that don't require parentheses ([[https:// | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | This RFC allows to omit parentheses around the '' | + | This RFC allows to omit parentheses around the '' |
<code php> | <code php> | ||
- | class MyClass | + | class MyClass |
{ | { | ||
const CONSTANT = ' | const CONSTANT = ' | ||
Line 60: | Line 60: | ||
new MyClass()-> | new MyClass()-> | ||
new MyClass()(), | new MyClass()(), | ||
+ | new MyClass([' | ||
); | ); | ||
Line 70: | Line 71: | ||
new $myClass()-> | new $myClass()-> | ||
new $myClass()(), | new $myClass()(), | ||
+ | new $myClass([' | ||
); | ); | ||
Line 79: | Line 81: | ||
new (trim(' | new (trim(' | ||
new (trim(' | new (trim(' | ||
+ | new (trim(' | ||
); | ); | ||
</ | </ | ||
- | This RFC still does not allow to omit parentheses around the '' | + | This RFC does not change behavior of '' |
- | this leads to an ambiguity: | + | |
<code php> | <code php> | ||
- | // Access property on a new instance or instantiate the result of expression? | + | new MyClass:: |
- | new MyClass:: | + | new $myClass:: |
- | new $myClass:: | + | new MyClass:: |
- | new $myClass-> | + | new $myClass:: |
- | + | new $myObject-> | |
- | // Access method on a new instance or instantiate the result of expression? | + | new MyArrayConst[' |
- | new $myClass-> | + | new $myArray[' |
</ | </ | ||
Line 112: | Line 114: | ||
// string(8) " | // string(8) " | ||
new class { public function __invoke() { return ' | new class { public function __invoke() { return ' | ||
+ | // string(5) " | ||
+ | new class ([' | ||
); | ); | ||
</ | </ | ||
- | ===== Ambiguity? | + | ===== Why the proposed syntax is unambiguous |
- | At first glance '' | + | At first glance '' |
- | '' | + | is it '' |
- | does not exist: | + | of class '' |
- | constructor arguments, not as instantiation of '' | + | is because PHP interprets the first expression after '' |
- | Here's how it is solved | + | Consider also '' |
- | The formula for the '' | + | It is interpreted as '' |
- | is a variable expression without calls. | + | The same is true for '' |
+ | |||
+ | Here's how it looks at the [[https:// | ||
+ | The formula for the '' | ||
+ | where '' | ||
<code php> | <code php> | ||
- | // class_name | ||
new MyClass(); | new MyClass(); | ||
+ | ^ | ||
+ | | | ||
+ | |—T_NEW | ||
+ | | | ||
+ | |—class_name | ||
+ | |||
- | // new_variable | ||
new $class(); | new $class(); | ||
+ | ^ | ||
+ | | | ||
+ | |—T_NEW | ||
+ | | | ||
+ | |—new_variable (cannot have calls!) | ||
- | // (expr) | ||
new (trim(' | new (trim(' | ||
+ | ^ | ||
+ | | | ||
+ | |—T_NEW | ||
+ | | | ||
+ | |—(expr) | ||
</ | </ | ||
- | This guarantees that the '' | + | Once the parser encounters '' |
- | whatever comes next a class name. Hence it's safe and unambiguous to further use the '' | + | This guarantees that the '' |
- | own without parentheses. It's like refactoring | + | without |
- | nobody will ever read it as '' | + | are crucial for the proposed syntax: parentheses denote the end of the class name and the end of the new expression. |
+ | |||
+ | ===== Other syntax ideas ===== | ||
+ | |||
+ | Some of the ideas expressed during the discussion of this RFC are listed below. They are orthogonal to this proposal and | ||
+ | require a separate RFC. | ||
+ | |||
+ | ==== Allow to omit the new keyword ==== | ||
+ | |||
+ | Some languages like Kotlin allow to instantiate classes via [[https:// | ||
+ | Omitting the " | ||
+ | (in Kotlin [[https:// | ||
+ | deprecate declaring functions and classes with the same names. | ||
+ | |||
+ | ==== MyClass:: | ||
+ | |||
+ | Introducing a dedicated static constructor like '' | ||
+ | break for already existing static or object methods named '' | ||
+ | type different from '' | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 149: | Line 188: | ||
PHP 8.4 | PHP 8.4 | ||
+ | |||
+ | ===== Proposed Voting Choices ===== | ||
+ | |||
+ | This is a simple yes-or-no vote to include this feature. 2/3 majority required to pass. | ||
+ | |||
+ | Voting started on 2024-05-09 and will end on 2024-05-24 00:00 GMT. | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | ===== Implementation ===== | ||
+ | |||
+ | Pull request contains the final implementation and plenty of tests asserting the expected behavior and backward compatibility: | ||
+ | |||
+ | ===== References ===== | ||
+ | |||
+ | * [[https:// | ||
+ | |||
+ | Old requests for this feature: | ||
+ | |||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | |||
rfc/new_without_parentheses.1712452777.txt.gz · Last modified: 2024/04/07 01:19 by vudaltsov