Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -4128,16 +4128,21 @@ 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);
}

if (def_value) {
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));
}
}
/* }}} */

Expand Down
27 changes: 27 additions & 0 deletions ext/reflection/tests/gh12856.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
GH-12856 (ReflectionClass::getStaticPropertyValue() returns UNDEF zval for uninitialized typed properties)
--FILE--
<?php

class Bug {
private static $untyped;
private static int $typed1;
private static int $typed2 = 3;
}

$rc = new ReflectionClass(Bug::class);
var_dump($rc->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)