Download the PHP package yolo-fx/di without Composer
On this page you can find all versions of the php package yolo-fx/di. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package di
PHP dependency injection
This is a simple and efficient PHP dependency injection package that uses the latest feature of PHP: annotations. Through annotations, we can mark a class as a singleton.
PHP version
Minimum PHP version support: 8.0
Introduction
- No dependencies
- Support singleton mode
- Support class alias
- Support class mappings
-
Support initializer method
Different from the constructor method of the class, it will be executed after the constructor method is executed (if any)
-
Support custom property injection
Implement
PropertyAttributeInterface
- Reflection object caching eliminates the need to repeatedly create the same class reflection and improves performance
- Circular dependency detection, capable of detecting circular dependency problems (including direct and indirect circular dependencies)
Installation
Usage
- Create an instance of a class.
You can use the Yolo\Di\DI::use()
to create an instance of the class, like:
Yolo\Di\DI::use(User::class)
- Singleton
You can use the Yolo\Di\Annotations\Singleton
annotation to mark a class as a singleton.
- Initializer
You can use the Yolo\Di\Annotations\Initializer
annotation to mark a method as an initializer.
- Custom property injection
Define a custom property injection attribute class that implements PropertyAttributeInterface
.
Add the custom property injection attribute class to DI.
And now, you can use the Cache
attribute in your class.
So the $logger
is a FileLogger
instance.
- Use class Mappings
This is useful when your constructor parameter is an interface type. For example:
You can you use the Yolo\Di\DI::bind()
to set class mappings.
So the LoggerInterface::class
will be replaced by ConsoleLogger::class
.
Now, you can use the Yolo\Di\DI::use()
to create an instance of the class.
DI::bind()
will affect the global class mapping. If you only want this effect on a certain instance, please use custom property attribute injection.
- Use class alias
Example
-
Create a class: UserTest.php
-
Create another class: AnimalTest.php
- Use DI to create an instance
Notes
- Do not have circular dependencies, including direct and indirect circular dependencies, such as A depends on B and B depends on A.