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/08 06:01] – Fixed creation date vudaltsov | rfc:new_without_parentheses [2024/05/28 18:04] (current) – Added Target Version vudaltsov | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Date: 2023-12-29 | * 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 and satisfy many users' requests for this feature (see [[https:// | * make coding with PHP more convenient and satisfy many users' requests for this feature (see [[https:// | ||
- | * decrease the visual debt ([[https:// | + | * decrease the visual debt in all sorts of builders and configurators |
* 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 59: | Line 60: | ||
new MyClass()-> | new MyClass()-> | ||
new MyClass()(), | new MyClass()(), | ||
+ | new MyClass([' | ||
); | ); | ||
Line 69: | Line 71: | ||
new $myClass()-> | new $myClass()-> | ||
new $myClass()(), | new $myClass()(), | ||
+ | new $myClass([' | ||
); | ); | ||
Line 78: | 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> | ||
- | // Instantiate and then access the instance or instantiate the result of the expression? | + | new MyClass:: |
- | new MyClass:: | + | new $myClass:: |
- | new MyClass:: | + | new MyClass:: |
- | new $myClass::CONSTANT; | + | new $myClass::$staticProperty; // will continue to work as `new ($myClass:: |
- | new $myClass:: | + | new $myObject-> |
- | new $myClass-> | + | new MyArrayConst[' |
- | new $myClass->method(); | + | new $myArray[' |
</ | </ | ||
Line 111: | Line 114: | ||
// string(8) " | // string(8) " | ||
new class { public function __invoke() { return ' | new class { public function __invoke() { return ' | ||
+ | // string(5) " | ||
+ | new class ([' | ||
); | ); | ||
</ | </ | ||
Line 157: | Line 162: | ||
without any additional parentheses. It also becomes clear why '' | without any additional parentheses. It also becomes clear why '' | ||
are crucial for the proposed syntax: parentheses denote the end of the class name and the end of the new expression. | 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 165: | 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.1712556064.txt.gz · Last modified: 2024/04/08 06:01 by vudaltsov