github-actions · GitHub

https://wiki.php.net/rfc/lazy-objects

https://github.com/php/php-src/blob/fb0ced92729db43b33ec583a3fec1a74dd56e43d/Zend/zend_lazy_objects.c#L19-L36

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 by unset() 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 __get or 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.

Read the original on github.com ↗