Download the PHP package ocramius/lazy-property without Composer
On this page you can find all versions of the php package ocramius/lazy-property. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ocramius/lazy-property
More information about ocramius/lazy-property
Files in ocramius/lazy-property
Package lazy-property
Short Description A library that provides lazy instantiation logic for object properties
License MIT
Homepage https://github.com/Ocramius/LazyProperty
Informations about the package lazy-property
Lazy Property
This small library aims at providing a very simple and efficient loading of lazy properties
Abandoned
Starting with PHP 8.3, dynamic properties are no longer allowed "out of the box".
While it is still possible to have dynamic properties via explicit declaration (the #[\AllowDynamicProperties]
attribute), the approach
of this package is no longer to be considered safe nor efficient long-term.
Based on that, this package is deprecated and abandoned: please use traditional PHP array
s instead.
Installation
The suggested installation method is via composer:
Use case
In many cases where lazy-initialization of private/protected properties is necessary, many people write classes that look like following:
This is problematic because implementors and people subclassing SomeService
will eventually
write:
This can work only if SomeService#getDependency()
was called at least once upfront (which
may well be under certain circumstances), and therefore is a cause of bugs/headaches/suicides/etc.
In order to avoid this problem, the implementor of SomeService
that is also exposing
its protected $dependency
property may just use LazyProperty\LazyPropertiesTrait
to fix the problem:
With this, any access to SomeService#$dependency
will cause a call to
SomeService#getDependency()
if the property was not already initialized.
Please note that a getter is required in order for the property to be lazy.
Performance notes
Using LazyProperty\LazyPropertiesTrait
allows to speed up applications where a massive
amount of getter calls is going on in private/protected scope.
There is some minor overhead in calling SomeService#initLazyProperties()
, as well as in
the first property access, but it should be negligible.