Skip to content

Fix GH-12856: ReflectionClass::getStaticPropertyValue() returns UNDEF zval for uninitialized typed properties #17590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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_exception_ex(reflection_exception_ptr, 0,
"Property %s::$%s is not initialized and no default was provided", ZSTR_VAL(ce->name), ZSTR_VAL(name));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should say "Typed property (...)" to emphasize that this is for typed props?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May we not just use the existing message that is already used when uninitialized accessing typed properties?

Typed static property C::$prop must not be accessed before initialization

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, and then just as an Error rather than a ReflectionException or not?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it to the standard wording. Although I like the custom message that I had put (as it indicates why the exception is thrown more precisely), I see the appeal of using the standard wording.

} 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 (ReflectionException $e) {
echo $e->getMessage(), "\n";
}
var_dump($rc->getStaticPropertyValue('typed1', 1));
var_dump($rc->getStaticPropertyValue('typed2'));

?>
--EXPECT--
NULL
Property Bug::$typed1 is not initialized and no default was provided
int(1)
int(3)
Loading