Behaviour of ReflectionClass::getStaticPropertyValue() for uninitialized typed properties

From: Date: Sun, 26 Jan 2025 18:45:07 +0000
Subject: Behaviour of ReflectionClass::getStaticPropertyValue() for uninitialized typed properties
Groups: php.internals 
Request: Send a blank email to internals+get-126224@lists.php.net to get a copy of this message
Hi internals I'm looking for feedback on a bugfix for https://github.com/php/php-src/issues/12856. Fix PR: https://github.com/php/php-src/pull/17590 Basically, calling ReflectionClass::getStaticPropertyValue() on an uninitialized typed property currently returns a value of type UNDEF. UNDEF is a special internal type that should _never_ find its way into user code. The reason this happens is because of an implementation oversight: uninitialized typed properties are simply not handled. If we compare the behaviour with ReflectionClass::getValue(), then we observe that getValue() throws the typical "must not be accessed before initialization" error when accessing an uninitialized typed property. We could make getStaticPropertyValue() throw this as well, but it gets a bit awkward because it has a "default value" argument. I.e. its signature is: getStaticPropertyValue(string $name, mixed &$def_value = ?): mixed The purpose of $def_value is that if no property named $name exists, then $def_value is returned. If no $def_value is provided, then a ReflectionException is thrown with the message "Property ... does not exist". The awkwardness with making it consistent with getvalue() would be that we would throw on an uninitialized property even if a $def_value is provided. So the alternative idea, the one implemented in my PR, would be this: - If the property is uninitialized, and $def_value is not provided, then throw a ReflectionException with message "Property ... is not initialized and no default was provided". - If the property is uninitialized, and $def_value IS provided, then return $def_value. - In all other cases, the behaviour stays the same as it was. There was also some discussion about the pros and cons of different ideas in the issue thread. What do y'all think? Kind regards Niels

« previous php.internals (#126224) next »