Download the PHP package fanqingxuan/di without Composer
On this page you can find all versions of the php package fanqingxuan/di. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package di
中文文档地址
a IOC container for php
This is a extension which implements Dependency Injection, it's itself a container and it implements the
Inversion of Control pattern.
enviroment requirement
php7.0+
Install
If you want install it as php extension, please see di-ext. The same usage with this package.
Basic Usage
like you can see,there are serveral ways to register services as the follow list.
-
string
-
object instance
- Closures/Anonymous functions
You can pass additonal parameters to closure function.
also,you can pass parameter by using the key word of use.
Advanced Usage
-
Constructor Injection
This is injection type can pass arguments to the class constructor.
We can register the service this way.
-
Setter Injection
Some class have setters for injection with their specail demand. We modify the above service as the class with setters.
A service with setter injection can be registered as follows:
-
Properties Injection
You can inject parameters directly into public attributes of the class:
A service with properties injection can be registered as follows
More Advanced Usage
The next we will give a way that inject service from php file.
We can inject it to container as follows:
Array Syntax Usage
We introduce the usage with the set function above. In fact,the array syntax is also allowed to register as services.
Property Syntax Usage
We can inject the object with the property usage. Actually,it use the magic methd to realize it.
Get Services
-
get method
-
magic method
-
array-access syntax
- property syntax
Shared Services
Services can be registered as ‘shared’ services this means that they always will act as singletons. Once the service is resolved for the first time the same instance of it is returned every time.
or use the set method with its third parameter as 'true'.
Modify the Services
When the service is registered in the container,you can get it and modify it.
Automatic Inject the DI Container into the service
DI Container is used for inject other service into it. but sometimes the service itself need the the other instance from the container. If a class or component requires the DI itself to locate services, the DI can automatically inject itself to the instances it creates, to do this, you need to extends the JsonDi\Di\AbstractInjectionAware class in your classes:
Service Providers
Using the JsonDi\Di\ServiceProviderInterface you now register services by context. You can move all your $di->set()
calls to classes as follows. Notice return void for the register function.