Download the PHP package bogdanpet/orthite-di without Composer
On this page you can find all versions of the php package bogdanpet/orthite-di. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bogdanpet/orthite-di
More information about bogdanpet/orthite-di
Files in bogdanpet/orthite-di
Package orthite-di
Short Description Simple dependency injection container
License MIT
Informations about the package orthite-di
orthite-di
Simple dependency injection container
Instalation
Usage
Automatic resolving of classes
For example let's say we have model User, which depends on Request and Database objects, and the Database class depends on PDO.
Without DI creating $user object will look like this.
Now imagine if there were more dependendcies with more dependencies. This is where auto resolving comes to aid. All you need is to call Container's get() method. If the object is not in the definitions container will try to resolve it using type hinting.
Container will recursively try to resolve all the classes. Problem with this one is that PDO depends on dsn, user and password input which cannot be resolved. So some user defined parameters can still be passed. Pass the array of parameters and Container will know where to put them. Keep in mind that array keys must match the parameter names.
or
Assuming that you have variables $dsn, $username and $passwd defined.
Package also provide container() helper function if you prefer not to instantiate Container singleton. It will be done automatically inside the function.
Invoking a method from a class
Same way you resolve classes, methods from classes can also be resolved. For example, let's say class User has a method show which displays user account details, and it depends on user's id and Response class.
Container's method call() can be used to invoke the method and container will take care of resolving both class and method dependencies.
or with helper function cintainer_call()