Re: [RFC][Under discussion] Fetch properties in const expressions

From: Date: Fri, 03 Jun 2022 14:35:21 +0000
Subject: Re: [RFC][Under discussion] Fetch properties in const expressions
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-117850@lists.php.net to get a copy of this message
On Fri, Jun 3, 2022 at 3:26 PM Robert Landers <landers.robert@gmail.com> wrote: > > 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(); ``` but as https://externals.io/message/113347#113607 says, "side-effecting constructors are a terrible idea", and same can be said for "side-effecting [magic] getters"... Also note that this only concerns *global* (or namespace-scoped) constants (which can also be defined "dynamically" with define()), not *class* constants. -- Guilliam Xavier

« previous php.internals (#117850) next »