thecodingmachine / lazy-array
This package provides an array than can lazily instantiate the objects it contains.
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: ^4.5
- satooshi/php-coveralls: ^1.0
This package is auto-updated.
Last update: 2024-11-06 23:52:34 UTC
README
What is it?
This project contains a single class that behaves like an array. However, objects in this array can be instantiated only when the key in the array is fetched, so you don't have to create the instance right away. This is useful for performance considerations.
How does it work?
Easy, you create a new LazyArray
object, and then, you push objects in it.
$lazyArray = new LazyArray(); $key = $lazyArray->push(MyServiceProvider::class); // This will trigger the creation of the MyServiceProvider object and return it. $serviceProvider = $lazyArray[$key];
You can also pass parameters to the constructor of the object:
$lazyArray = new LazyArray(); $key = $lazyArray->push(MyServiceProvider::class, "param1", "param2");
And because we are kind, you can also push into the lazy array an already instantiated object:
$lazyArray = new LazyArray(); // This is possible, even if we loose the interest of the LazyArray. $key = $lazyArray->push(new MyServiceProvider());
Finally, if you are performance oriented (and I'm sure you are), you can create the whole lazy array in one call:
$lazyArray = new LazyArray([ MyServiceProvider::class, // Is you simply want to create an instance without passing parameters [ MyServiceProvider2::class, [ "param1", "param2 ] ], // Is you simply want to create an instance and pass parameters to the constructor new MyServiceProvider4('foo') // If you directly want to push the constructed instance. ]);
Why?
As the examples suggest, this was built for improving the performance of service providers loading (in compiled container environment). Do not use this as a replacement for a dependency injection container, this is a bad idea.