Download the PHP package hatem-azzouzi/z-di without Composer
On this page you can find all versions of the php package hatem-azzouzi/z-di. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hatem-azzouzi/z-di
More information about hatem-azzouzi/z-di
Files in hatem-azzouzi/z-di
Package z-di
Short Description Easy minimalist dependenncy injection package
License MIT
Informations about the package z-di
Z-DI
Contents of this file
- Introduction
- Requirements
- Installation
- Usage
- Dependency Injection
- Lazy loading
- Eager loading
- Recursion
- Contributing
- Maintainers
- License
Introduction
Z-DI is intended for educational purposes for beginners looking to learn more skills and master PHP. Very lightweight and minimalist DI (Dependency Injection) package with lazy loading and recursive configuration capabilities.
Requirements
There is no requirement to install this package other than PHP. PHP >=7.2.0 is recommended. PHP 8.3 is highly recommended.
Installation
Use the dependency manager composer to install Z-DI.
Usage
Dependency Injection (DI)
DI is a technique to inject a resource or a service (dependency) into a client on which it depends on to function. Client does not have to create nor to instantiate the resource or know how it is created.
Below is an example of a configuration file to create entry points for 2 services and a client. In this example, we inject the Email service into the Client. You can also try the Text service. Both services implements the same interface IMessage.
To test the injection, run tests/injection/index.php.
Lazy loading
Lazy loading a resource is intended to differ loading or instantiating a resource until it is needed. This technique is for (web) applications where performance, high availability, acceleration and initialization times to be kept at a minimum are critical.
Below is an example of a configuration file to create an entry point for ClassA injection and its constructor parameters.
Run tests/lazy/index.php.
The ClassA is instantiated after making the first foo() function call. As long as foo() is not called, the ClassA will never load.
Eager loading
Alternatively, eager loading is to load the resource as soon as the bootstrap is started. In our example above, instead of just referencing the resource, it will be instantiated using instance() function as shown below.
The ClassA is instantiated before making the first foo() function call.
NOTE: In our injection example above we have used lazy loading, if we rather would like to use eager loading, we need to inject the instantiated service when instantiating the client.
Recursion
Injection can be replaced recursively based on the environment variable such as staging or production, the hostname or any other specific configuration. The first element in the array is the default injections configuration file.
To test the recursion..
Default configuration
Dev configuration
Production configuration
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.