Skip to content

[RFC] Lazy Objects #15019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 56 commits into from
Closed

[RFC] Lazy Objects #15019

wants to merge 56 commits into from

Conversation

arnaud-lb
Copy link
Member

@arnaud-lb arnaud-lb commented Jul 18, 2024

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

/* Lazy objects are standard zend_object whose initialization is defered until
* one of their properties backing store is accessed for the first time.
*
* This is implemented by using the same fallback mechanism as __get and __set
* magic methods that is triggered when an undefined property is accessed.
*
* Execution of methods or virtual property hooks do not trigger initialization.
*
* A lazy object can be created via the Reflection API. The user specifies an
* initializer function that is called when initialization is required.
*
* There are two kinds of lazy objects:
*
* - Ghosts: These are initialized in-place by the initializer function
* - Proxy: The initializer returns a new instance. After initialization,
* interaction with the proxy object are proxied to the instance.
*
* Internal objects are not supported.

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.

Copy link
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not managed to finish the review today, I'll continue tomorrow.

Copy link
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM otherwise!

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

Successfully merging this pull request may close these issues.

2 participants