Download the PHP package quillstack/di without Composer
On this page you can find all versions of the php package quillstack/di. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download quillstack/di
More information about quillstack/di
Files in quillstack/di
Package di
Short Description The dependency injection container based on PSR-11: Container interface.
License MIT
Homepage https://quillstack.org/di
Informations about the package di
Quillstack DI Container
Quillstack DI Container is the dependency injection container based on PSR-11: Container interface, and with the main goal: to be fast. You can find the full documentation on the website: \ https://quillstack.org/di
This DI container uses constructors and types of class properties.
Installation
To install this package, run the standard command using Composer:
Usage
You can use Quillstack DI Container when you want:
- To have a simple and fast DI container.
- Define dependencies based on interfaces.
- Define parameters e.g. credentials for a database in the
Database
class. - To use constructors or/and class properties.
- To implement your own instance factories e.g. for
Request
classes. - To use objects as dependencies.
Simple usage
You can easily start using a DI Container:
Where your ExampleController
class looks like:
Dependencies based on interfaces
If you want to define which class should be loaded based on an interface:
You can define your dependencies using interfaces:
When you create the object using the DI container, the type of $logger
property will be set to Logger
.
Dependencies with parameters
If some of your classes require parameters, define them as an array passed on the second parameter to the container:
Every time you will get a database object, a container will use localhost
as
a value for $hostname
parameter:
Custom instance factories
You can implement your own instance factory. This is especially useful when you want to create many objects in a class family that are very similar in some way.
In our example we want to create different request objects:
First, we had to create RequestInterface
as a common interface for all requests.
Next, we have to create an instance factory class. To create it, extend a class with CustomFactoryInterface
:
Also, use this configuration array when you create a DI container:
Custom factories are useful for objects you want to create similarly.
Dependencies as objects
In this example, whenever a new class of LoggerInterface will be required as a dependency, a container will use a previously defined object. This object can be created once in a bootstrap file and used in the entire application:
This configuration is helpful if an object should be created once and its instance should be used in other places in the application.
Unit tests
Run tests using a command: