Download the PHP package terrydjony/ion without Composer
On this page you can find all versions of the php package terrydjony/ion. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download terrydjony/ion
More information about terrydjony/ion
Files in terrydjony/ion
Package ion
Short Description Ion is a minimalist inversion of control container using automatic dependency injection
License MIT
Informations about the package ion
ION Container
A minimalist inversion of control container using automatic dependency injection
Installation
To install ION Container, you need to add it to your composer.json file
Or, by using terminal
Usage
First, you need to instantiate the container
Instantiating Object
To instantiate any objects, you should use make($class, array $args = array())
, instead of new
keyword
Notice that the parameter two $args
is optional
For example, if you want to instantiate stdClass
class.
Registering Service
You can register the way to instantiate some class via register($key, $closure)
Here is an example of registering PDO class with some defined arguments
The above example will register PDO service. So, if you instantiate it via $ion->make('PDO')
, you don't need to supply any arguments since all the arguments have been defined before.
However, it will be nice to have a database config.
Notice that if you register a service, it'll be a shared instance. If you want to register a factory, use registerFactory
Registering Factory Services
You can register a factory using registerFactory($key, $closure)
Registering Parameters
You can also register parameters using setParam($param, $value)
and get the value via param($param_name)
Forcing New Instance
You can force a new instance using makeNew($key, $args=array())
.
Defining Default Class For Interface
You can bind an interface to a class. So, if a class depends on that interface, that class will represent that interface
If the class must be instantiated with some arguments, you should register it first. Then, you give the key as the parameter two of bindInterface
But, if the class can be instantiated with no arguments, you can just specify the class name.
Automatic Dependency Injection
Suppose you want to instantiate a class A that needs class B which needs class C
I mean something like
And, to make a class A instance, you just need to:
And, voila! You get the class A instance!
If you have a dependency that needs some parameters, you can register the dependency first before making object that needs that dependency
And, if the class needs some arguments, you can supply an array of arguments as parameter two of make or makeNew method.
The class A instance is made automatically.