https://github.com/thecodingmachine/lazy-array
This package provides an array than can lazily instantiate the objects it contains.
https://github.com/thecodingmachine/lazy-array
Last synced: 3 months ago
JSON representation
This package provides an array than can lazily instantiate the objects it contains.
- Host: GitHub
- URL: https://github.com/thecodingmachine/lazy-array
- Owner: thecodingmachine
- Created: 2016-04-12T13:55:05.000Z (about 10 years ago)
- Default Branch: 1.0
- Last Pushed: 2016-04-12T14:09:41.000Z (about 10 years ago)
- Last Synced: 2025-02-16T12:30:28.384Z (over 1 year ago)
- Language: PHP
- Size: 5.86 KB
- Stars: 1
- Watchers: 8
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://scrutinizer-ci.com/g/thecodingmachine/lazy-array/?branch=1.0)
[](https://travis-ci.org/thecodingmachine/lazy-array)
[](https://coveralls.io/github/thecodingmachine/lazy-array?branch=1.0)
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.
```php
$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:
```php
$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:
```php
$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:
```php
$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.