On Mon, Apr 8, 2024, at 6:08 AM, Valentin Udaltsov wrote:
Hello internals,
I would like to propose a syntax change for PHP 8.4 that allows to
immediately access instantiated objects without wrapping the expression
into parentheses.
This was requested and discussed several times, see:
Here's what you will be able to write after this change:
class MyClass
{
const CONSTANT = 'constant';
public static $staticProperty = 'staticProperty';
public static function staticMethod(): string { return 'staticMethod'; }
public $property = 'property';
public function method(): string { return 'method'; }
public function __invoke(): string { return '__invoke'; }
}
var_dump(
new MyClass()::CONSTANT, // string(8) "constant"
new MyClass()::$staticProperty, // string(14) "staticProperty"
new MyClass()::staticMethod(), // string(12) "staticMethod"
new MyClass()->property, // string(8) "property"
new MyClass()->method(), // string(6) "method"
new MyClass()(), // string(8) "__invoke"
);
On Mon, Apr 8, 2024, at 6:08 AM, Valentin Udaltsov wrote:
Hello internals,
I would like to propose a syntax change for PHP 8.4 that allows to
immediately access instantiated objects without wrapping the expression
into parentheses.
This was requested and discussed several times, see:
Here’s what you will be able to write after this change:
class MyClass
{
const CONSTANT = 'constant';
public static $staticProperty = 'staticProperty';
public static function staticMethod(): string { return 'staticMethod'; }
public $property = 'property';
public function method(): string { return 'method'; }
public function __invoke(): string { return '__invoke'; }
}
var_dump(
new MyClass()::CONSTANT, // string(8) "constant"
new MyClass()::$staticProperty, // string(14) "staticProperty"
new MyClass()::staticMethod(), // string(12) "staticMethod"
new MyClass()->property, // string(8) "property"
new MyClass()->method(), // string(6) "method"
new MyClass()(), // string(8) "__invoke"
);
I always thought there was some technical parser reason why this wasn’t possible. Maybe that was true in 5.x but isn’t anymore?
I cannot speak to the implementation, but I’m all for the change itself.
–Larry Garfield
Hi, Larry! The grammar is compiled with no warnings, so it is definitely possible now. I also added a lot of tests that guarantee that existing behaviour is preserved and new syntax works as expected.
Marco, Bilge, Levi, Larry, thank you for your positive feedback on the RFC.
On Tue, Apr 23, 2024 at 11:10 AM Valentin Udaltsov
<udaltsov.valentin@gmail.com> wrote:
вт, 9 апр. 2024 г. в 19:41, Larry Garfield <larry@garfieldtech.com>:
On Mon, Apr 8, 2024, at 6:08 AM, Valentin Udaltsov wrote:
> Hello internals,
>
>
>
> I would like to propose a syntax change for PHP 8.4 that allows to
> immediately access instantiated objects without wrapping the expression
> into parentheses.
>
>
>
> This was requested and discussed several times, see:
>
> - Allow (...)->foo() expressions not only for `new` - Externals
>
> - PHP :: Request #70549 :: Allow `new Foo()->bar()` without parens around `(new Foo)`
>
> - Suggested change: change priority of new and -> - Externals
>
> - Raising the precedence of the new operator - Externals
>
>
>
> Here's what you will be able to write after this change:
>
> ```php
>
> class MyClass
>
> {
>
> const CONSTANT = 'constant';
>
> public static $staticProperty = 'staticProperty';
>
> public static function staticMethod(): string { return 'staticMethod'; }
>
> public $property = 'property';
>
> public function method(): string { return 'method'; }
>
> public function __invoke(): string { return '__invoke'; }
>
> }
>
>
>
> var_dump(
>
> new MyClass()::CONSTANT, // string(8) "constant"
>
> new MyClass()::$staticProperty, // string(14) "staticProperty"
>
> new MyClass()::staticMethod(), // string(12) "staticMethod"
>
> new MyClass()->property, // string(8) "property"
>
> new MyClass()->method(), // string(6) "method"
>
> new MyClass()(), // string(8) "__invoke"
>
> );
>
> ```
>
>
>
> For more details see the RFC: PHP: rfc:new_without_parentheses
>
> Implementation: new MyClass()->method() without parentheses by vudaltsov · Pull Request #13029 · php/php-src · GitHub
I always thought there was some technical parser reason why this wasn't possible. Maybe that was true in 5.x but isn't anymore?
I cannot speak to the implementation, but I'm all for the change itself.
--Larry Garfield
Does anyone have additional feedback? I'd like to start voting on Thursday, April 25th.
I've also added a section on other syntax ideas that have been expressed on Twitter and in the PR: PHP: rfc:new_without_parentheses
--
Valentin
I suspect this will break (badly written) reflection in interesting ways:
This basically breaks dereferencing order of operations and makes it
inconsistent.
On Mon, Apr 8, 2024, at 6:08 AM, Valentin Udaltsov wrote:
Hello internals,
I would like to propose a syntax change for PHP 8.4 that allows to
immediately access instantiated objects without wrapping the expression
into parentheses.
This was requested and discussed several times, see:
Here’s what you will be able to write after this change:
class MyClass
{
const CONSTANT = 'constant';
public static $staticProperty = 'staticProperty';
public static function staticMethod(): string { return 'staticMethod'; }
public $property = 'property';
public function method(): string { return 'method'; }
public function __invoke(): string { return '__invoke'; }
}
var_dump(
new MyClass()::CONSTANT, // string(8) "constant"
new MyClass()::$staticProperty, // string(14) "staticProperty"
new MyClass()::staticMethod(), // string(12) "staticMethod"
new MyClass()->property, // string(8) "property"
new MyClass()->method(), // string(6) "method"
new MyClass()(), // string(8) "__invoke"
);
This basically breaks dereferencing order of operations and makes it
inconsistent.
Robert Landers
Software Engineer
Utrecht NL
I think these scenarios are all covered in the RFC under “This RFC still does not allow to omit parentheses around the new expression without constructor arguments’ parentheses, because in some cases this leads to an ambiguity”
This basically breaks dereferencing order of operations and makes it
inconsistent.
Quote from RFC:
“RFC still does not allow to omit parentheses around the new expression without constructor arguments’ parentheses, because in some cases this leads to an ambiguity”
And actually it mentions a list in the RFC:
// Instantiate and then access the instance or instantiate the result of the expression?
new MyClass::[CONSTANT](http://www.php.net/constant);
new MyClass::$staticProperty;
new $myClass::[CONSTANT](http://www.php.net/constant);
new $myClass::$staticProperty;
new $myClass->property;
new $myClass->method();
But actually from all of those, right now, only “new MyClass::CONSTANT;” and “new $myClass::CONSTANT;” are not working, while the other 4 are working fine.
So yeah, this needs clarification if they continue to work as they work right now: https://3v4l.org/PmCfR
On Thu, May 9, 2024, at 3:26 PM, Valentin Udaltsov wrote:
@Lynn, @Alex, thank you for your comments. I have improved the "without
constructor arguments' parentheses" part of the introduction section
and started the voting.
I believe standard procedure expects you to post a new message to the list, entitled `[RFC] {Vote] some title`, to announce the start of the vote and the date it will end.
@Lynn, @Alex, thank you for your comments. I have improved the “without constructor arguments’ parentheses” part of the introduction section and started the voting.