Skip to content

ReflectionProperty::isInitialized() no longer initializes lazy objects once its cache slot is warm (8.5 regression) #25238

Closed

Description

@GaryPEGEOT

Description

Description

Since PHP 8.5, a ReflectionProperty that is reused across several lazy ghosts of the same class only triggers initialization for the first one. For every later ghost, isInitialized() returns false and the object stays uninitialized, although the initializer would assign the property.

The Lazy Objects design wants isInitialized() to initialize the object before answering, because it observes the object's state (see the maintainer's answer in #20830, which was closed as not planned for that reason). PHP 8.4 behaves that way. 8.5 does it only until the reflection object's inline cache is populated.

The following code:

<?php

final class Line
{
    public string $label = '';
}

$class = new ReflectionClass(Line::class);
$ghost = static fn (string $label): Line => $class->newLazyGhost(static function (Line $line) use ($label): void {
    $line->label = $label;
});

$property = new ReflectionProperty(Line::class, 'label');

foreach (['first', 'second', 'third'] as $label) {
    $line = $ghost($label);
    printf("%s: isInitialized()=%s, still lazy=%s\n",
        $label,
        var_export($property->isInitialized($line), true),
        var_export($class->isUninitializedLazyObject($line), true),
    );
}

Resulted in this output (PHP 8.5.4 and 8.5.12, with php -n):

first: isInitialized()=true, still lazy=false
second: isInitialized()=false, still lazy=true
third: isInitialized()=false, still lazy=true

But I expected this output instead (what PHP 8.4.27 prints):

first: isInitialized()=true, still lazy=false
second: isInitialized()=true, still lazy=false
third: isInitialized()=true, still lazy=false

A fresh new ReflectionProperty(...) per object gives the expected output on 8.5 too, as does calling isInitialized() twice on the same ghost.

Probable cause: #17698 ("Use new ZPP and inline caches in ReflectionProperty::getValue() and variants") added this fast path to ReflectionProperty::isInitialized() in ext/reflection/php_reflection.c (still present on master and PHP-8.5):

if (ref->cache_slot[0] == Z_OBJCE_P(object)) {
    uintptr_t prop_offset = (uintptr_t) ref->cache_slot[1];

    if (EXPECTED(IS_VALID_PROPERTY_OFFSET(prop_offset))) {
        zval *value = OBJ_PROP(Z_OBJ_P(object), prop_offset);
        RETURN_BOOL(!Z_ISUNDEF_P(value));
    }
}

The properties of an uninitialized lazy object are UNDEF, so once the first call (through has_property(), which initializes the object) has filled the cache slot, later objects of the same class get false without going through has_property(). The same PR's fast path in getValue() only returns when the slot is not UNDEF and otherwise falls back to the handler, which is why getValue() is not affected. Doing the same in isInitialized(), returning true from the fast path only when the slot is set and falling back to has_property() otherwise, or skipping the fast path for lazy objects, should fix it.

Impact: libraries that cache ReflectionProperty instances read lazy objects as empty. For example, the Symfony Validator (PropertyMetadata::getPropertyValue() checks isInitialized() with a cached ReflectionProperty and returns null when it is false). Every lazy ghost after the first of its class is then validated as if its typed properties were unset. This shows up with Symfony JsonStreamer, which builds the objects of a list as lazy ghosts, and likely with Doctrine ORM's native lazy proxies.

PHP Version

PHP 8.5.12 (cli) (built: Nov  5 2026 07:20:51) (NTS)

Also reproduced on 8.5.4. Not reproduced on 8.4.27.

Operating System

Linux (official php:8.5-cli and php:8.4-cli Docker images; also a local build)

PHP Version

PHP 8.5.12 (cli) (built: Nov  5 2026 07:20:51) (NTS)
Copyright (c) The PHP Group
Built by https://github.com/docker-library/php
Zend Engine v4.5.12, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.12, Copyright (c), by Zend Technologies

Operating System

Ubuntu 26.04

Activity

  1. added a commit that references this issue on Nov 6, 2026
    020d474
  2. added 2 commits that reference this issue on Nov 6, 2026
    0e66515
    088f299
  3. added a commit that references this issue on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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