diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index f5e463699b1b5..724de0a491240 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -4128,7 +4128,7 @@ ZEND_METHOD(ReflectionClass, getStaticPropertyValue) prop = zend_std_get_static_property(ce, name, BP_VAR_IS); EG(fake_scope) = old_scope; - if (prop) { + if (prop && !Z_ISUNDEF_P(prop)) { RETURN_COPY_DEREF(prop); } @@ -4136,8 +4136,13 @@ ZEND_METHOD(ReflectionClass, getStaticPropertyValue) RETURN_COPY(def_value); } - zend_throw_exception_ex(reflection_exception_ptr, 0, - "Property %s::$%s does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name)); + if (prop) { + zend_throw_error(NULL, + "Typed property %s::$%s must not be accessed before initialization", ZSTR_VAL(ce->name), ZSTR_VAL(name)); + } else { + zend_throw_exception_ex(reflection_exception_ptr, 0, + "Property %s::$%s does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name)); + } } /* }}} */ diff --git a/ext/reflection/tests/gh12856.phpt b/ext/reflection/tests/gh12856.phpt new file mode 100644 index 0000000000000..519a222ab0ed7 --- /dev/null +++ b/ext/reflection/tests/gh12856.phpt @@ -0,0 +1,27 @@ +--TEST-- +GH-12856 (ReflectionClass::getStaticPropertyValue() returns UNDEF zval for uninitialized typed properties) +--FILE-- +getStaticPropertyValue('untyped')); +try { + var_dump($rc->getStaticPropertyValue('typed1')); +} catch (Error $e) { + echo $e->getMessage(), "\n"; +} +var_dump($rc->getStaticPropertyValue('typed1', 1)); +var_dump($rc->getStaticPropertyValue('typed2')); + +?> +--EXPECT-- +NULL +Typed property Bug::$typed1 must not be accessed before initialization +int(1) +int(3)