https://wiki.php.net/rfc/lazy-objects
The implementation is quite simple:
- A lazy object is just a normal object (with standard handlers, etc), with all properties unset. One could create such object in userland by using
ReflectionClass::newInstanceWithoutConstructor(), followed byunset()on all properties. - We hook in zend_std_read_property / zend_std_write_property and so on, in the code path that handles accesses to undefined properties (where we may call
__getor trigger warnings). When accessing an undefined property for an object marked lazy, we call the initializer function, and restart the operation.
The engine doesn't need to be aware of lazy objects at all: everything is handled in standard object handlers.
zend_lazy_objects.c implements the logic of making an object lazy, or initializing a lazy object. Functions in these files are called by either zend_object_handlers.c, or ext/reflection/php_reflection.c.