Re: [RFC][Under discussion] Fetch properties in const expressions
| From: | Ilija Tovilo | Date: | Tue, 14 Jun 2022 16:13:24 +0000 |
| Subject: | Re: [RFC][Under discussion] Fetch properties in const expressions | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-117947@lists.php.net to get a copy of this message | ||
Hi Robert, hi Guilliam
> > So, how does this change our mental models when writing PHP? For
> > example, I generally consider
const to be a
> > "compile-time" constant
> > (ie, generated/created before any code is actually executed). So would
> > this allow code to actually execute before any other code (ie, by
> > putting code in a __get()) and thus cause issues due to database
> > drivers and etc to not be loaded yet -- or cause them to be loaded
> > prematurely?
>
> True but that's already the case since PHP 8.1 and
> https://wiki.php.net/rfc/new_in_initializers
> (https://github.com/php/php-src/pull/7153), e.g.
> https://3v4l.org/MSWi0:
>
> ```
> class Foo {
> public function __construct() {
> echo "Side effect!\n";
> }
> }
>
> const C = new Foo();
> ```
Exactly. In fact, the only way you could invoke arbitrary userland
code through __get is in combination with new, where side-effects
are already possible via constructor. Enums themselves don't allow
__get and thus can't lead to side-effects in constant expressions.
If there are no other concerns I'd like to open the voting phase by
the end of the week.
Ilija