Download the PHP package metrophp/metrodi without Composer
On this page you can find all versions of the php package metrophp/metrodi. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package metrodi
metrodi
Small, smart dependency injector
tl;dr
- Doesn't use class types
- Doesn't rely on autoloading
- Checks parameter names first
- Does actually use class types, but only after checking parameter names
- Auto injects any public class variable ending in 'Service'
Setup
Metro DI can be used by creating an object or as a singleton with globally namespaced functions.
With global functions it looks like this:
Things
In Metro DI you name your dependencies with the DI def function. An undefined thing will return a prototype object that logs all method calls used against it via the magic __call.
Constructor Injection
When an object is created with _make, the injector will inspect constructor arguments and find any defined things with the same name and pass them. Why not use class types? Well, imagine trying to switch an actual object implementation. You would have to change every file that has the old class type hint, or you would have to create a library full of interfaces in order to get ready for any potential switch in the future.
Some other languages don't have the benefit of being typeless. Using the name of the variable instead of the type hint (while still falling back to the type hint) allows for the most rapid prototyping and the most flexible changing of dependency implementations down the line.
Passing Parameters
You can pass parameters both when defining an object and when making an object.
Singletons
Singletons and new objects can both be access with _make() but it depends on how you use _make() with _didef().
If your object is not inherently a service and needs new constructor params everytime you make it, you can pass constructor params for every _make() call. Each unique combinations of parameters will be hashed and the resulting object will be cached and return on subsequent calls to _make()
You can combine both methods by supplying parameters to the _didef() which will be combined and used as defaults to subsequent _make() calls
The _makeNew() function will always return a new instance of a class definition regardless of parameters passed. It simply bypasses any instance cache and proceeds to make a new object the same way it does the very first time _make() is called.
Closures
Closures or any callbacks can be passed as the definition of any things as of 3.1.0.
The result of the anonymous function will be return from _make instead of the function object. This behavior defaults to behaving like a singleton where repeated calls to _make('connection') will return the same object.
To get a new reference and have the anonymous function invoked again, use _makeNew()
Service Providers
When you are making a thing from a class that defines public class variables and one of those variable names ends in 'Service', the dependency injector will automatically inject a lazy loading object for that service onto the object's variable.