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
Hi Niels
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/17590So 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.
I agree that this is the right approach. The proposed behavior is also
in line with native property access, with or without ??.
https://3v4l.org/S3mf8
Ilija