Download the PHP package hjerichen/class-instantiator without Composer
On this page you can find all versions of the php package hjerichen/class-instantiator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hjerichen/class-instantiator
More information about hjerichen/class-instantiator
Files in hjerichen/class-instantiator
Package class-instantiator
Short Description A factory for objects. Instantiates any class.
License MIT
Informations about the package class-instantiator
Class Instantiator
The class instantiator tries to create an object of the passed class recursively.
Some arguments for a constructor cannot be created automatically.
For example primitive types like integer or interfaces.
Or you want to inject a specific object, that should not be created automatically, like a PDO object.
For those scenarios, there are multiple solutions.
Pass predefined arguments:
Extend the class instantiator:
Use an attribute to define a class instantiator extension:
Instantiate classes with an extension has a higher priority then using an attribute.
This allows overwriting the attribute and instantiate classes depending on your current needs.
You can also use the instantiator attribute to let the class build from a method in a specific instantiator:
The attribute can also be used on a constructor parameter:
The attribute on the parameter has a higher priority as the one on the class itself.
It is also possible to specify a method of the instantiator in the attribute.
Storing Objects
It is possible to sore objects permanently with an attribute.
Those objects will then be returned when a matching class is requested instead of creating a new one every time.
Storing objects with attribute does not work when the instantiation is done in the extension.
This is because of the higher priority for extensions.
But you can store objects as well when using the method "injectObject" inside the creation method of the extension.
Inject Objects
It is possible to inject objects into the class instantiator.
Those objects will be returned when a matching class is requested.
PSR-11 Container
There is also a PSR-11 Container Implementation.
You can use this container in frameworks and libraries who support those.
Example for GraphQLite:
License and authors
This project is free and under the MIT Licence. Responsible for this project is Heiko Jerichen ([email protected]).