Skip to content

ReflectionProperty::isInitialized() should not trigger lazy object initialization #20830

Closed as not planned

Description

@greg0ire

Description

The following code:

<?php
class TestClass {
    public readonly int $id;
    public string $name;
}
$rc = new ReflectionClass(TestClass::class);
$obj = $rc->newLazyGhost(function($ghost) {
    $rp = new ReflectionProperty(TestClass::class, 'id');
    $rp->setRawValueWithoutLazyInitialization($ghost, 123);
    $ghost->name = 'initialized';
    echo "Ghost initialized\n";
});
echo "Created lazy ghost\n";
echo "Is uninitialized: " . ($rc->isUninitializedLazyObject($obj) ? 'yes' : 'no') . "\n";
// Checking if a readonly property is initialized should NOT trigger initialization
$rp = new ReflectionProperty(TestClass::class, 'id');
echo "Checking ReflectionProperty::isInitialized()...\n";
$result = $rp->isInitialized($obj);
echo "isInitialized returned: " . ($result ? 'true' : 'false') . "\n";
echo "Is still uninitialized: " . ($rc->isUninitializedLazyObject($obj) ? 'yes' : 'no') . "\n";

Resulted in this output:

Created lazy ghost
Is uninitialized: yes
Checking ReflectionProperty::isInitialized()...
Ghost initialized
isInitialized returned: true
Is still uninitialized: no

But I expected this output instead:

Created lazy ghost
Is uninitialized: yes
Checking ReflectionProperty::isInitialized()...
isInitialized returned: false
Is still uninitialized: yes

Note: I couldn't find any discussion about this, so maybe it's an oversight. However the Lazy Objects RFC (https://wiki.php.net/rfc/lazy-objects) explicitly states under "Initialization Triggers":

Except for the special cases listed below, any attempt to observe the state of a lazy object will trigger its initialization. This ensures that the result of the observation is the same as if the object were already initialized, maintaining full transparency.

These triggers include, but are not limited to:

  • Reading or writing a property
  • Testing if a property is set or unsetting it
  • Calling ReflectionProperty::get[Raw]Value() and set[Raw]Value()
  • Calling ReflectionObject::getProperties() or ReflectionObject::getProperty()

isInitialized() is not listed, so it's unclear whether this is intentional or not.

For more context about this, see doctrine/orm#12166

PHP Version

PHP 8.4.16 (cli) (built: Dec 16 2025 16:03:34) (NTS gcc x86_64)
Copyright (c) The PHP Group
Built by Fedora Project
Zend Engine v4.4.16, Copyright (c) Zend Technologies

Operating System

Fedora

Activity

  1. arnaud-lb commented on Jan 6, 2026

    @arnaud-lb
    Member

    Hi @greg0ire,

    It's not an oversight.

    The general principle of Lazy Objects is that any attempt to observe their state should trigger their initialization before the observation is performed. This ensures that Lazy Objects are fully transparent: operations on them have the same outcome as if they were normal objects.

    In this case, ReflectionProperty::isInitialized() checks whether a property has a value. This is observing the object's state, so the object is initialized before performing the operation. It makes sense because doing otherwise could result in a different outcome, compared to a normal object, as the initializer may assign a value to the property.

    Unfortunately the name ReflectionProperty::isInitialized() can be confused with the Lazy Objects API, but in fact it's not. It doesn't check whether a property is Lazy: It checks whether a property has a value.

    Looking at your example, it seems odd that $rp->setRawValueWithoutLazyInitialization($ghost, 123); is called in the initializer, and not outside of it. When this method is called outside, the code behaves as you expect.

  2. greg0ire commented on Jan 6, 2026

    @greg0ire
    ContributorAuthor

    Looking at your example, it seems odd that $rp->setRawValueWithoutLazyInitialization($ghost, 123); is called in the initializer, and not outside of it. When this method is called outside, the code behaves as you expect.

    Ah so maybe we can fix the issue by changing https://github.com/doctrine/orm/blob/8cbd34c6668fea94ed5cdb2d19a7aa7a405b30aa/src/Proxy/ProxyFactory.php#L210-L242 then?

    Unfortunately the name ReflectionProperty::isInitialized() can be confused with the Lazy Objects API, but in fact it's not. It doesn't check whether a property is Lazy: It checks whether a property has a value.

    It's confusing indeed but I don't think (🤞 ) I got confused here: if an object is an uninitialized lazy object, I'd expect none of its properties to be initialized either. If they were initialized, then it would mean something would have written the property, which should have triggered the initialization, right? So it's not like things would behave differently whether the object is lazy or not, I think.

  3. arnaud-lb commented on Jan 6, 2026

    @arnaud-lb
    Member

    Ah so maybe we can fix the issue by changing https://github.com/doctrine/orm/blob/8cbd34c6668fea94ed5cdb2d19a7aa7a405b30aa/src/Proxy/ProxyFactory.php#L210-L242 then?

    Possibly yes.

    I understand that ->isInitialized() is called in https://github.com/doctrine/orm/blob/2ca63df90cae8e203f6f1a2b12eb3e394e3d1f21/src/Mapping/PropertyAccessors/ReadonlyAccessor.php#L29

    as a result of calling ->setValue() in https://github.com/doctrine/orm/blob/2ca63df90cae8e203f6f1a2b12eb3e394e3d1f21/src/Proxy/ProxyFactory.php#L227

    $classMetadata->propertyAccessors[$idField] is either a RawValuePropertyAccessor or a ReadonlyAccessor.

    RawValuePropertyAccessor uses ReflectionProperty::setRawValueWithoutLazyInitialization() directly, when the object is not an old proxy.

    ReadonlyAccessor uses ReflectionProperty::isInitialized() and ReflectionProperty::setValue(), both of which would trigger initialization of the object.

    ProxyFactory should always use setRawValueWithoutLazyInitialization().

    if an object is an uninitialized lazy object, I'd expect none of its properties to be initialized either.

    This is true, internally, but Lazy Objects are designed so that this is not observable (except via specific Lazy Object APIs). What is observable is whether the property is initialized on the initialized object.

    The following example prints bool(true) bool(false) because prop1 is initialized and prop2 is not, on the initialized object:

    class C {
        public int $prop2;
        public function __construct(public int $prop1) {}
    }
    
    $reflector = new ReflectionClass(C::class);
    $obj = $reflector->newLazyGhost(function ($obj) {
        $obj->__construct(0);
    });
    
    var_dump((new ReflectionProperty(C::class, 'prop1'))->isInitialized($obj)); // bool(true)
    var_dump((new ReflectionProperty(C::class, 'prop2'))->isInitialized($obj)); // bool(false)

    This code behaves the same whether $obj is lazy or not, as designed.

  4. greg0ire commented on Jan 6, 2026

    @greg0ire
    ContributorAuthor

    OK, well, thanks for your insight, I will try to apply it on Doctrine, and I'll take this as an occasion to thank you for the lazy objects feature in full as well, it's had an awesome effect on Doctrine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions