[RFC] Constant Scalar Expressions (with constants)

php.internals

Bob Weinand

12 years ago
Hi! As you all know, Anthony Ferrara had withdrawn his RFC a few months ago, which was without support for constants in these expressions: const foo = 10; const bar = 10 * foo; The use of a constant in a declaration of a constant was not yet possible with his patch. That's what I changed. Enjoy reading the RFC at: https://wiki.php.net/rfc/const_scalar_exprs Bob Weinand Special thanks go to Dmitry who had helped me a lot with feedback and a deep review of the patch.

Lars Strojny

12 years ago
Hi Bob, thanks for your work, it’s awesome. One thing about constants: Take this example: const FOO = 2; class Klass { const FOO = 1; const BAR = FOO; } Here FOO is ambiguous, as it could resolve to 2 or to 1. If I understood it correctly, global constants are not yet implemented but I would suggest to always force prefixing the constant to get rid of the ambiguity and allow for future extension. const FOO = 2; class Klass { const FOO = 1; const BAR = self::FOO; } Does that make sense? cu, Lars On 07 Nov 2013, at 13:01, Bob Weinand <bobwei9@hotmail.com> wrote:

Nikita Popov

12 years ago
On Thu, Nov 7, 2013 at 1:31 PM, Lars Strojny <lars@strojny.net> wrote:
> Hi Bob, > > thanks for your work, it’s awesome. One thing about constants: > > Take this example: > > const FOO = 2; > > class Klass > { > const FOO = 1; > const BAR = FOO; > } > > Here FOO is ambiguous, as it could resolve to 2 or to 1. If I understood > it correctly, global constants are not yet implemented but I would suggest > to always force prefixing the constant to get rid of the ambiguity and > allow for future extension. > > const FOO = 2; > > class Klass > { > const FOO = 1; > const BAR = self::FOO; > } > > Does that make sense? >
The code snippets you posted are *already* supported (e.g. in PHP 5.5). FOO will resolve to the globals constant, self::FOO to the class constant. Presumably the extended support in static expressions will retain those semantics :) Nikita

Sebastian Krebs

12 years ago
2013/11/7 Lars Strojny <lars@strojny.net>
> Hi Bob, > > thanks for your work, it’s awesome. One thing about constants: > > Take this example: > > const FOO = 2; > > class Klass > { > const FOO = 1; > const BAR = FOO; > } > > Here FOO is ambiguous, as it could resolve to 2 or to 1. If I understood > it correctly, global constants are not yet implemented but I would suggest > to always force prefixing the constant to get rid of the ambiguity and > allow for future extension. > > const FOO = 2; > > class Klass > { > const FOO = 1; > const BAR = self::FOO; > } > > Does that make sense? >
Yes, also from the consistency point of view: I'd expected to refer class constants via its class name (or self/static). Having to ways to refer them is confusing.
> > cu, > Lars > > On 07 Nov 2013, at 13:01, Bob Weinand <bobwei9@hotmail.com> wrote: > > > Hi! > > > > As you all know, Anthony Ferrara had withdrawn his RFC a few months ago, > which was without support for constants in these expressions: > > > > const foo = 10; > > const bar = 10 * foo; > > > > The use of a constant in a declaration of a constant was not yet > possible with his patch. > > > > That's what I changed. > > > > Enjoy reading the RFC at: > > > > https://wiki.php.net/rfc/const_scalar_exprs > > > > Bob Weinand > > > > Special thanks go to Dmitry who had helped me a lot with feedback and a > deep review of the patch. > > -- > > PHP Internals - PHP Runtime Development Mailing List > > To unsubscribe, visit: http://www.php.net/unsub.php > > > >
-- github.com/KingCrunch

Bob Weinand

12 years ago
Hi Lars, In what way is this ambiguous? I just tested; FOO never equals self::FOO. FOO always is a global constant, while self::FOO (and Klass::FOO) is the only way to refer to the class constant. const FOO = 2; class Klass { const FOO = 1; const BAR = FOO; // value two, global constant, and if FOO is not defined, Klass::Bar === "FOO". } Bob Am 7.11.2013 um 13:31 schrieb Lars Strojny <lars@strojny.net>:

Lars Strojny

12 years ago
Hi Bob, alright, my fault not getting the ternary operator example :) Thanks for the clarification! Lars On 07 Nov 2013, at 13:45, Bob Weinand <bobwei9@hotmail.com> wrote:

Remi Collet

12 years ago
Le 07/11/2013 13:01, Bob Weinand a écrit :
> Enjoy reading the RFC at: > > https://wiki.php.net/rfc/const_scalar_exprs
I have enjoy it. Really nice. Remi.

Patrick Schaaf

12 years ago
Am 07.11.2013 13:01 schrieb "Bob Weinand" <bobwei9@hotmail.com>:
> > https://wiki.php.net/rfc/const_scalar_exprs
Wow, this looks really nice! Will this work with class autoloading, too? i.e. will class foo { const X = bar::X; } trigger autoloading for class bar? best regards Patrick

Nikita Popov

12 years ago
On Thu, Nov 7, 2013 at 2:08 PM, Patrick Schaaf <bof@bof.de> wrote:
> Am 07.11.2013 13:01 schrieb "Bob Weinand" <bobwei9@hotmail.com>: > > > > https://wiki.php.net/rfc/const_scalar_exprs > > Wow, this looks really nice! > > Will this work with class autoloading, too? i.e. will > > class foo { > const X = bar::X; > } > > trigger autoloading for class bar? >
Once again, the code you posted is already supported, not really part of this proposal. And yes, it will invoke the autoloader, see http://codepad.viper-7.com/2Xz8ux :) Nikita

Van Peer Ilias

12 years ago
It's nice to see people get excited for things they already have. Should lead to a little more appreciation of what's already there, I guess.. -----Original Message----- From: Nikita Popov [mailto:nikita.ppv@gmail.com] Sent: donderdag 7 november 2013 14:13 To: Patrick Schaaf Cc: Bob Weinand; internals Subject: Re: [PHP-DEV] [RFC] Constant Scalar Expressions (with constants) On Thu, Nov 7, 2013 at 2:08 PM, Patrick Schaaf <bof@bof.de> wrote:
> Am 07.11.2013 13:01 schrieb "Bob Weinand" <bobwei9@hotmail.com>: > > > > https://wiki.php.net/rfc/const_scalar_exprs > > Wow, this looks really nice! > > Will this work with class autoloading, too? i.e. will > > class foo { > const X = bar::X; > } > > trigger autoloading for class bar? >
Once again, the code you posted is already supported, not really part of this proposal. And yes, it will invoke the autoloader, see http://codepad.viper-7.com/2Xz8ux :) Nikita This e-mail and all files transmitted as attachment(s) thereto are confidential and solely intended for the individual to whom or the organization to which they are addressed. If you received this e-mail by mistake, please notify Cegeka's Service Desk at cegeka.support@cegeka.be or call +32 (0)11 240 363. We thank you in advance. Cegeka hereby confirms that this message has been swept by Sophos for the presence of viruses.

Patrick Schaaf

12 years ago
Am 07.11.2013 14:12 schrieb "Nikita Popov" <nikita.ppv@gmail.com>:
> > Once again, the code you posted is already supported, not really part of
this proposal. Blush. I'm still sitting at 5.4.x :)
> And yes, it will invoke the autoloader, see
http://codepad.viper-7.com/2Xz8ux :) "Interesting" line number in that error message..... best regards Patrick

Bob Weinand

12 years ago
Am 7.11.2013 um 14:19 schrieb Patrick Schaaf <bof@bof.de>:
> Am 07.11.2013 14:12 schrieb "Nikita Popov" <nikita.ppv@gmail.com>: >> >> Once again, the code you posted is already supported, not really part of > this proposal. > > Blush. I'm still sitting at 5.4.x :)
That's no excuse :-P The only thing that RFC introduces are operations on constants and constant values on definitions of constants/properties/statics. Not more.
>> And yes, it will invoke the autoloader, see > http://codepad.viper-7.com/2Xz8ux :) > > "Interesting" line number in that error message.....
Yes, class constants are evaluated upon their first use. Bob

Derick Rethans

12 years ago
On Thu, 7 Nov 2013, Bob Weinand wrote:
> Hi! > > As you all know, Anthony Ferrara had withdrawn his RFC a few months ago, which was without support for constants in these expressions: > > const foo = 10; > const bar = 10 * foo; > > The use of a constant in a declaration of a constant was not yet possible with his patch. > > That's what I changed. > > Enjoy reading the RFC at: > > https://wiki.php.net/rfc/const_scalar_exprs
https://github.com/bwoebi/php-src/compare/const_scalar_exprs#diff-4d0b93094aafe8cf519b4f0a3322f471L590 -#define IS_CALLABLE 10 +#define IS_CONSTANT_AST 10 +#define IS_CALLABLE 11 Can I please suggest that you do not change existing constant values? You're using a macro to check for the constant type anyway, so you can do the complex checking for constant types in there. I'm also of the opinion that just " This can allow for writing far easier to understand code, by allowing for far more expressive code." is by far not enough of a justification for a syntax addition. Please please try to be convincing by having good arguments, presenting alternatives and cons. cheers, Derick
-- http://derickrethans.nl | http://xdebug.org Like Xdebug? Consider a donation: http://xdebug.org/donate.php twitter: @derickr and @xdebug Posted with an email client that doesn't mangle email: alpine

Bob Weinand

12 years ago
Am 7.11.2013 um 14:34 schrieb Derick Rethans <derick@php.net>:
> On Thu, 7 Nov 2013, Bob Weinand wrote: >> Hi! >> >> As you all know, Anthony Ferrara had withdrawn his RFC a few months ago, which was without support for constants in these expressions: >> >> const foo = 10; >> const bar = 10 * foo; >> >> The use of a constant in a declaration of a constant was not yet possible with his patch. >> >> That's what I changed. >> >> Enjoy reading the RFC at: >> >> https://wiki.php.net/rfc/const_scalar_exprs > > https://github.com/bwoebi/php-src/compare/const_scalar_exprs#diff-4d0b93094aafe8cf519b4f0a3322f471L590 > > -#define IS_CALLABLE 10 > +#define IS_CONSTANT_AST 10 > +#define IS_CALLABLE 11 > > Can I please suggest that you do not change existing constant values? > You're using a macro to check for the constant type anyway, so you can > do the complex checking for constant types in there.
Just comparing twice (> and <) should be faster than comparing three times (three times an ==). Dmitry had suggested this to me. And I don't see the big disadvantage here as IS_CALLABLE is anyway just used for the typehint. Nothing there which relies on the fact that IS_CALLABLE equals to 10.
> I'm also of the opinion that just " This can allow for writing far > easier to understand code, by allowing for far more expressive code." is > by far not enough of a justification for a syntax addition. Please > please try to be convincing by having good arguments, presenting > alternatives and cons.
Well, that's _the_ justification. I can also list other justifications, but that'd be just other ways to express the same, so I just kept it simple. Alternatives… don't really exist. (at least not in function signatures for example) And cons? Can't think of any problem with it. Bob