Hi, 😃 👋
This is a minimalistic PHP project with the aim to provide a simple but powerful solution to anyone that thinks Dependency Injection (DI) is something useful.
PHPDI is a DI container, allowing you to control your dependencies throughout your whole project. I won't explain here what DI and containers are in detail. For more information, you can visit this site 👉 HERE
The whole design behind the realisation is Convention over Configuration. That doesn't mean that you won't need to make some tweeks and adjustments, but you won't need to do anything special.
Currently there are two versions of the project: This is the official second version. The second version contains everything from the first one.
Version one will not be maintained anymore.
Beta state means, that everything tested so far works, but that there might be some small bugs.
Alpha state means, that it is still in phase of testing and might be unstable, but that you can always give it a try and report issues, bugs, and missing features.
Config options | Description | Object type (Injection) |
---|---|---|
className | Name of the class to inject | Object, with parameterless constructor |
isSingleton | Creates singleton object | Object, with parameterless constructor |
Config options | Description | Object type (Injection) |
---|---|---|
name | Name of the value | Value type object |
type | Type of the object (string, int and so on) | Value type object |
value | Value, desired to be returned | Value type object |
Config options | Description | Object type (Injection) |
---|---|---|
className | Name of the class to inject | Object, with constructor with parameters |
isSingleton | Creates singleton object | Object, with constructor with parameters |
params | The params for constructor | Object, with constructor with parameters |
In the params, the root node is the name of parameter in the constructor and in it the defaulValue.
IMPORTANT: The config currently does not allow the use of reference type parameters when calling an injection. To get a reference type object when calling a dependency, you should use the container in the constructor in the class of the dependency and set its properties with the injected objects there.
See the examples in the project.
There is an AutoLoader, which loads the classes automatically just when needed. Just specify the path in the LoaderConfig.php
file once.
You can easily switch between different configured injection configurations for development, testing, and production.
Be sure that only allowed classes can create instances and use object dependencies.
Option to log all injections - Which examples are used and where.
Option to log some catched exceptions - Which examples are used and where.
Option to check whether all injections have an interface inherited by them.
All these things, can be modified in the Config.php
Initially creates a proxy object which loads the whole object only when used.
The injection configs are now exported to json files, which makes working with different ones very easy.
composer require ivangrigorov/php-simple-dicontainer
Before use, you can delete the doc folder, index.php
, IURLParser.php
, and URLParser.php
(which are used only for the tests).
Update the config dependencies json file.
- In your composer.json file add
"autoload": { "classmap": ["vendor/ivangrigorov/php-simple-dicontainer/DI/Lib/"] }
- Run
composer update
command - In your initial file add
require __DIR__."/vendor/autoload.php";
- Get instance of the container (static method)
- Call the requested method with the correct parameters. (To get reference type object pass the className with the "I" prefix -> IclassName).
3.1. Example for parameter for getInjectionWithParams method -
["params" => array(["name" => "url"])]);
If "value" is not given, the default from the config is used.
If you've read everything up to this point, congratulations 👏 and thank you for your interest 👍
The license for this is open source and free to use. I will appreciate a star or a good word if you think it deserves it.
You can raise issues and report problems or missing features. You can help with ideas and solutions too.
For contact - ivangrigorov9 at gmail.com