[php-src] Issue #12856: ReflectionClass::getStaticPropertyValue() returns UNDEF zval for uninitialized typed properties

From: Date: Sat, 02 Dec 2023 21:50:44 +0000
Subject: [php-src] Issue #12856: ReflectionClass::getStaticPropertyValue() returns UNDEF zval for uninitialized typed properties
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-245962@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/12856 Author: ju1ius ### Description The following code: ```php <?php declare(strict_types=1); class Bug { private static $foo; private static int $bar; } $rc = new ReflectionClass(Bug::class); var_dump($rc->getStaticPropertyValue('foo')); var_dump($v = $rc->getStaticPropertyValue('bar')); echo "bar = {$v}\n"; ``` Resulted in this output: ``` NULL UNKNOWN:0 Warning: Undefined variable $v in ... on line 11 bar = ``` But I expected this output instead: Well TBH, I'm not sure... If the goal is to be consistent with ReflectionProperty::getValue(), then: ``` NULL Fatal error: Uncaught Error: Typed static property Bug::$bar must not be accessed before initialization in ... ``` Otherwise, then: ``` NULL NULL ``` Looking at [the implementation](https://heap.space/xref/php-src/ext/reflection/php_reflection.c?r=3433dab5#4154), the code calls zend_std_get_static_property(ce, name, BP_VAR_IS); which silences exceptions, but also [completely bypasses the typed property initialization check](https://heap.space/xref/php-src/Zend/zend_object_handlers.c?r=8a392edd#1629). So it looks like: * if the behaviour of ReflectionProperty::getValue() is desired, then the method should use BP_VAR_R instead of BP_VAR_IS (or just use zend_read_static_property_ex with silent=false) * otherwise the conditional in zend_std_get_static_property should be fixed to return a null pointer regardless of the BP_VAR_* mode. ### PHP Version PHP 8.3-dev ### Operating System irrelevant

« previous php.bugs (#245962) next »