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"
);
For more details see the RFC:
https://wiki.php.net/rfc/new_without_parentheses
Implementation: https://github.com/php/php-src/pull/13029
--
Best regards, Valentin
On Mon, Apr 8, 2024 at 3:11 AM Valentin Udaltsov <
udaltsov.valentin@gmail.com> 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" );
For more details see the RFC:
https://wiki.php.net/rfc/new_without_parenthesesImplementation: https://github.com/php/php-src/pull/13029
--
Best regards, Valentin
Yes, please!
--
Marco Deleu
I haven't verified the implementation, but as long as there really
aren't backwards compatibility issues for correct code, then this
would be a nice quality of life improvement.
Hi Valentin,
You don't need me to tell you how popular your PR is, since it's the
single most emoji'd open PR by a wide margin, but this is something that
has bugged me since the parser improvements in 5.4. I even remember
asking Nikita if it was possible to not have to wrap new Class
in
parens to dereference it, and he said it was, but just didn't do it for
whatever reason.
It's frustrating using a PHP REPL (e.g. Boris, PsySH) and wanting to
write new Class->foo()
only to have to backtrack and add the parens,
because it's not natural (for me) to think in terms of the parens ahead
of time. Even in the editor, it's the same experience; I often find
myself backtracking to add the parens. Perhaps this seems like a small
thing to some people, but for me this could be the sole reason to day-1
upgrade to 8.4.
But I have no voting privileges so I can only offer you my thanks.
Thanks for this!
Cheers,
Bilge
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" );
For more details see the RFC:
https://wiki.php.net/rfc/new_without_parenthesesImplementation: https://github.com/php/php-src/pull/13029
--
Best regards, Valentin
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" );
For more details see the RFC: https://wiki.php.net/rfc/new_without_parentheses
Implementation: https://github.com/php/php-src/pull/13029
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
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"
);
For more details see the RFC:
https://wiki.php.net/rfc/new_without_parentheses
Implementation: https://github.com/php/php-src/pull/13029
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.
вт, 9 апр. 2024 г. в 19:41, Larry Garfield larry@garfieldtech.com:
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"
);
For more details see the RFC:
https://wiki.php.net/rfc/new_without_parentheses
Implementation: https://github.com/php/php-src/pull/13029
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:
https://wiki.php.net/rfc/new_without_parentheses#other_syntax_ideas
Valentin
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:
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" );
For more details see the RFC: https://wiki.php.net/rfc/new_without_parentheses
Implementation: https://github.com/php/php-src/pull/13029
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: https://wiki.php.net/rfc/new_without_parentheses#other_syntax_ideasValentin
I suspect this will break (badly written) reflection in interesting ways:
This basically breaks dereferencing order of operations and makes it
inconsistent.
Robert Landers
Software Engineer
Utrecht NL
On Tue, Apr 23, 2024 at 1:20 PM Robert Landers landers.robert@gmail.com
wrote:
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:
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"
);
For more details see the RFC:
https://wiki.php.net/rfc/new_without_parentheses
Implementation: https://github.com/php/php-src/pull/13029
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:
https://wiki.php.net/rfc/new_without_parentheses#other_syntax_ideasValentin
I suspect this will break (badly written) reflection in interesting ways:
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"
On Tue, Apr 23, 2024 at 3:31 PM Robert Landers landers.robert@gmail.com
wrote:
On Tue, Apr 23, 2024 at 11:10 AM Valentin Udaltsov
udaltsov.valentin@gmail.com wrote: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:
https://wiki.php.net/rfc/new_without_parentheses#other_syntax_ideasValentin
I suspect this will break (badly written) reflection in interesting ways:
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
http://www.php.net/constant;" and "new $myClass::CONSTANT
http://www.php.net/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
Thanks,
Alex
@Lynn, @Alex, thank you for your comments. I have improved the "without
constructor arguments' parentheses" part of the introduction section and
started the voting.
вт, 23 апр. 2024 г. в 16:55, Alexandru Pătrănescu drealecs@gmail.com:
On Tue, Apr 23, 2024 at 3:31 PM Robert Landers landers.robert@gmail.com
wrote:On Tue, Apr 23, 2024 at 11:10 AM Valentin Udaltsov
udaltsov.valentin@gmail.com wrote: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:
https://wiki.php.net/rfc/new_without_parentheses#other_syntax_ideasValentin
I suspect this will break (badly written) reflection in interesting ways:
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
http://www.php.net/constant;" and "new $myClass::CONSTANT
http://www.php.net/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/PmCfRThanks,
Alex
--
С уважением, Валентин
@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.
--Larry Garfield
Yes, I've done that, see https://externals.io/message/123293.
чт, 9 мая 2024 г. в 20:11, Larry Garfield larry@garfieldtech.com:
@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.--Larry Garfield
--
С уважением, Валентин
Yes, I've done that, see https://externals.io/message/123293.
D'Oh. At almost the exact same time I replied. :-) Thanks.
(Also, please do not top post.)
--Larry Garfield