Download the PHP package cee/simple-di without Composer
On this page you can find all versions of the php package cee/simple-di. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cee/simple-di
More information about cee/simple-di
Files in cee/simple-di
Informations about the package simple-di
SimpleDi
Simple Dependency Injection Container
Useful for making dependency tree of a service classes. The Service class is a class which has only one instance in a application. It can be used for simple alternative to Nette Di Container. All configuration of the DI Container is in PHP language.
Basic usage
Example with interfaces as the dependencies
The Service class providing some functionality with dependencies on previous interfaces:
Implementations of an interfaces:
And finally the configuration of the Simple DI Container with created instance of the NotificationService
:
Simple Di Container fill in parameters by type hint (using PHP Reflection). This is called autowiring.
Example with no type hinted parameter in constructor of the service class
Container configuration created as extending class of the Simple DI Container:
And in application we are using own Container:
This example has disadvantage - NotificationService
is created at start of the application and not on demand as other service classes created by Simple DI Container. This is useful on old code without need refactoring code. But the goal of the refactoring is create wrapper on all no type hinted parameters of the service class. In this case first refactoring step is:
After this, you do not need create NotificationService by own. Next step is creating of the service class Logger as is used in example with interface.
Example with already created classes
Typical use case is an old smell code with singletons. Or if you need successive refactoring. You have already instance of the service class before you can initiate a DI Container. You need use autowiring of this class to other.
And it works: