Download the PHP package undercloud/reservoir without Composer
On this page you can find all versions of the php package undercloud/reservoir. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download undercloud/reservoir
More information about undercloud/reservoir
Files in undercloud/reservoir
Package reservoir
Short Description Reservoir - PHP DI (Dependency Injection)
License MIT
Homepage https://github.com/undercloud/reservoir
Informations about the package reservoir
Reservoir
Inspired by Laravel's Service Container
In software engineering, dependency injection is a technique whereby one object (or static method) supplies the dependencies of another object. A dependency is an object that can be used (a service).
Installation
composer require undercloud/reservoir
Usage
Create container instance:
Instance
You may bind an existing object instance into the container using the instance
method. The given instance will always be returned on subsequent calls into the container:
Singleton
The singleton
method binds a class or interface into the container that should only be resolved one time. Once a singleton binding is resolved, the same object instance will be returned on subsequent calls into the container:
Bind
We can register a binding using the bind
method, passing the class or interface name that we wish to register along with a Closure
that returns an instance of the class:
A very powerful feature of the service container is its ability to bind an interface to a given implementation:
Alias
To support both a class/interface and a short name simultaneously, use alias
:
Decorator
If you want to add additional functionality to an existing binding in the container, use the decorator
method:
Fork
Sometimes it is required to create a copy of an existing entity in a container, it is possible to do this via the fork
method, the existing binding will be retrieved from the container and cloned:
Resolve
make
Simple entity extraction:
Resolve class:
Resolve method:
Magic __invoke
Extract entity by Closure
:
Extra parameters
The make
method has a second $additional
argument that helps pass parameters that are not in the container:
makes
Retrieving entity list:
Binding Primitives
Sometimes you may have a class that receives some injected classes, but also needs an injected primitive value such as an integer. You may easily use contextual binding to inject any value your class may need:
Contextual Binding
Sometimes you may have two classes that utilize the same interface, but you wish to inject different implementations into each class:
ServiceProvider
All service providers extend the Reservoir\ServiceProvider
class. Most service providers contain a register
method:
Register service provider:
Deferred providers
If your provider is only registering bindings in the service container, you may choose to defer its registration until one of the registered bindings is actually needed. Deferring the loading of such a provider will improve the performance of your application, since it is not loaded from the filesystem on every request:
Register deferred service provider:
Utils
has
Check if key registered:
keys
Get all registered keys:
forget
Remove instance:
flush
Clear all registered keys: