# [RFC] Constant Scalar Expressions (with constants)

- Thread: https://externals.io/message/70048
- Original archive: https://news-web.php.net/php.internals/70048
- Started by Bob Weinand on 2013-11-07T12:01:11+00:00
- Messages: 14
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #70048 — Bob Weinand — 2013-11-07T12:01:11+00:00

URL: https://externals.io/message/70048#70048

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.

---

## #70049 — Lars Strojny — 2013-11-07T12:31:14+00:00

URL: https://externals.io/message/70048#70049 · In reply to: #70048

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



> 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.

---

## #70050 — Nikita Popov — 2013-11-07T12:41:55+00:00

URL: https://externals.io/message/70048#70050 · In reply to: #70049

> 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

---

## #70051 — Sebastian Krebs — 2013-11-07T12:43:15+00:00

URL: https://externals.io/message/70048#70051 · In reply to: #70049

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
>
> 
>
> > […]
> which was without support for constants in these expressions:
> > […]
> possible with his patch.
> > […]
> deep review of the patch.
> > […]
>
>


-- 
github.com/KingCrunch

---

## #70052 — Bob Weinand — 2013-11-07T12:45:25+00:00

URL: https://externals.io/message/70048#70052 · In reply to: #70049

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>:

> 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
> 
> 
> 
> > […]

---

## #70053 — Remi Collet — 2013-11-07T13:03:44+00:00

URL: https://externals.io/message/70048#70053 · In reply to: #70048

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.

---

## #70054 — Patrick Schaaf — 2013-11-07T13:08:52+00:00

URL: https://externals.io/message/70048#70054 · In reply to: #70048

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

---

## #70055 — Lars Strojny — 2013-11-07T13:10:11+00:00

URL: https://externals.io/message/70048#70055 · In reply to: #70052

Hi Bob,

alright, my fault not getting the ternary operator example :)

Thanks for the clarification!
Lars



> 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>:
> 
> > […]

---

## #70056 — Nikita Popov — 2013-11-07T13:12:57+00:00

URL: https://externals.io/message/70048#70056 · In reply to: #70054

> Am 07.11.2013 13:01 schrieb "Bob Weinand" <bobwei9@hotmail.com>:
> > […]
>
> 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

---

## #70057 — Van Peer Ilias — 2013-11-07T13:15:44+00:00

URL: https://externals.io/message/70048#70057 · In reply to: #70056

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)



> Am 07.11.2013 13:01 schrieb "Bob Weinand" <bobwei9@hotmail.com>:
> > […]
>
> 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.

---

## #70058 — Patrick Schaaf — 2013-11-07T13:19:57+00:00

URL: https://externals.io/message/70048#70058 · In reply to: #70056

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

---

## #70059 — Bob Weinand — 2013-11-07T13:27:32+00:00

URL: https://externals.io/message/70048#70059 · In reply to: #70058

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>:
> > […]
> 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.

> > […]
> http://codepad.viper-7.com/2Xz8ux :)
> 
> "Interesting" line number in that error message.....

Yes, class constants are evaluated upon their first use.

Bob

> 
> best regards
>  Patrick

---

## #70061 — Derick Rethans — 2013-11-07T13:34:09+00:00

URL: https://externals.io/message/70048#70061 · In reply to: #70048

> 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

---

## #70062 — Bob Weinand — 2013-11-07T16:02:21+00:00

URL: https://externals.io/message/70048#70062 · In reply to: #70061

Am 7.11.2013 um 14:34 schrieb Derick Rethans <derick@php.net>:
> 
> > […]
> 
> 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

> 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
