Download the PHP package liftkit/dependency-injection without Composer
On this page you can find all versions of the php package liftkit/dependency-injection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download liftkit/dependency-injection
More information about liftkit/dependency-injection
Files in liftkit/dependency-injection
Package dependency-injection
Short Description Dependency injection library for LiftKit
License LGPL-2.1-only
Informations about the package dependency-injection
Dependency Injection
A simple but featured dependency injection library, with automatic class-resolution.
Create a new container
Rules
A rule is an anonymous function that defines how to create an object.
Singleton rules
A rule will execute each time getObject
is called by default. In order to force it to
execute only once, we use setSingletonRule
. Each call to getObject
for the rule will return
the same object.
Rules with parameters
Some rules can have parameters passed to them. The first argument to the setRule
callback is the
container istelf. Each subsequent argument is supplied by an optional array of parameters supplied
to getObject
.
Rules that reference other rules
Overriding rules
Rules can be overridden by redefining them. This is useful for modular code.
Storing an instance
You can also store an object you've already created an bind it to a rule.
Automatic resolution
The container can also bind a rule to a class. An instance of a class
can be created automatially by looking at the typehints of each
constructor argument. In the case below, and instance of B
is
automatically created before creating an instance of A
. The newly-created instance of B is then injected into the constructor of A
.
Automatic resolution with rules
In some cases, you may need to tell the injector to create an instance following a different rule when it encounters the
typehint of a certain class instead. In the example below, a rule is created for the construction of B
. When the container realizes it needs an instance of B
when creating an A
, it will follow that rule to create B
first. In this case, A
's constructor was injected with an instance of B created by the rule 'GiveMeANewB'
to create $a
.
Automatic resolution with parameters
Sometimes, there are additional parameters that need to be passed to the constructor of a new instance that is being automatically constructed. Below the variables $param1
and $param2
will be injectied into C
's constructor, while B
will be created by the rule 'GiveMeANewB'
above. Any additional parameters must fall at the end of the constructor's list of parameters.
Binding classes to aliases
Sometimes, you may want the container to resolve to a subclass when it encounters a particular typehint. In the example below, a new instance of D
will be injected into A
, instead of an instance of B
.