Skip to content

ReflectionClass::getStaticPropertyValue() returns UNDEF zval for uninitialized typed properties #12856

@ju1ius

Description

@ju1ius

Description

The following code:

<?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, 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.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions