You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ReflectionProperty::isInitialized() should not trigger lazy object initialization #20830
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.
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.
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.
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.
$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:
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.
Activity
arnaud-lb commentedon Jan 6, 2026
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.greg0ire commentedon Jan 6, 2026
Ah so maybe we can fix the issue by changing https://github.com/doctrine/orm/blob/8cbd34c6668fea94ed5cdb2d19a7aa7a405b30aa/src/Proxy/ProxyFactory.php#L210-L242 then?
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.
arnaud-lb commentedon Jan 6, 2026
Possibly yes.
I understand that
->isInitialized()is called in https://github.com/doctrine/orm/blob/2ca63df90cae8e203f6f1a2b12eb3e394e3d1f21/src/Mapping/PropertyAccessors/ReadonlyAccessor.php#L29as a result of calling
->setValue()in https://github.com/doctrine/orm/blob/2ca63df90cae8e203f6f1a2b12eb3e394e3d1f21/src/Proxy/ProxyFactory.php#L227$classMetadata->propertyAccessors[$idField]is either aRawValuePropertyAccessoror aReadonlyAccessor.RawValuePropertyAccessorusesReflectionProperty::setRawValueWithoutLazyInitialization()directly, when the object is not an old proxy.ReadonlyAccessorusesReflectionProperty::isInitialized()andReflectionProperty::setValue(), both of which would trigger initialization of the object.ProxyFactoryshould always usesetRawValueWithoutLazyInitialization().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)becauseprop1is initialized andprop2is not, on the initialized object:This code behaves the same whether
$objis lazy or not, as designed.greg0ire commentedon Jan 6, 2026
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.