ocramius / lazy-property
A library that provides lazy instantiation logic for object properties
Fund package maintenance!
Ocramius
Installs: 70 890
Dependents: 2
Suggesters: 0
Security: 0
Stars: 69
Watchers: 3
Forks: 8
Open Issues: 7
Requires
- php: ~8.1.0 || ~8.2.0
Requires (Dev)
- doctrine/coding-standard: ^10.0.0
- phpunit/phpunit: ^9.5.26
- roave/infection-static-analysis-plugin: ^1.26.0
- vimeo/psalm: ^5.0.0
- 2.6.x-dev
- 2.5.x-dev
- 2.5.0
- 2.4.x-dev
- 2.4.0
- 2.3.x-dev
- 2.3.0
- 2.2.x-dev
- 2.2.0
- 2.1.x-dev
- 2.1.0
- 2.0.x-dev
- 2.0.0
- 1.0.1
- 1.0.0
- dev-renovate/phpunit-phpunit-10.x
- dev-renovate/all-minor-patch
- dev-renovate/actions-checkout-4.x
- dev-renovate/lock-file-maintenance
- dev-renovate/doctrine-coding-standard-12.x
- dev-dependabot/composer/vimeo/psalm-4.9.1
- dev-dependabot/composer/phpunit/phpunit-9.5.8
- dev-dependabot/composer/infection/infection-0.22.0
- dev-dependabot/add-v2-config-file
This package is auto-updated.
Last update: 2023-09-19 12:15:00 UTC
README
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:
composer require ocramius/lazy-property
Use case
In many cases where lazy-initialization of private/protected properties is necessary, many people write classes that look like following:
class SomeService { protected $dependency; public function doWork() { $this->getDependency()->delegateWork(); } protected function getDependency() { return $this->dependency ?: $this->dependency = get_dependency_somehow(); } }
This is problematic because implementors and people subclassing SomeService
will eventually
write:
class SomethingElse extends SomeService { public function doOtherWork() { $this->dependency->doMoreWork(); } }
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:
#[\AllowDynamicProperties] class SomeService { use \LazyProperty\LazyPropertiesTrait; protected MyDependency $dependency; public function __construct() { $this->initLazyProperties(['dependency']); } public function doWork() { // look ma! no getter! $this->dependency->delegateWork(); } protected function getDependency() { return $this->dependency ?: $this->dependency = get_dependency_somehow(); } }
With this, any access to SomeService#$dependency
will cause a call to
SomeService#getDependency()
if the property was not already initialized.
class SomethingElse extends SomeService { public function doOtherWork() { // always works $this->dependency->doMoreWork(); } }
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.