Download the PHP package pyther/ioc without Composer
On this page you can find all versions of the php package pyther/ioc. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package ioc
Pyther.Ioc
A simple lightweight PHP Inversion of Control (IoC) container with the following features:
- allow the creation of multiple or singleton instances
- goes hand in hand with Constructor Dependency Injection
- can bind to classes, methods, functions and instances
- resolve nested dependencies, based on constructor DI
- can bind with optional arguments
- allow per instance arguments for non singletons during resolve phase
- detect cyclic dependencies on first use
- take default constructor arguments into account
- support for multiple, independent containers
- no external dependencies
- easy to use
Requirements
- php 8.1 or higher
Quickstart
Install the Composer Package
composer require pyther/ioc
And here is a fictional example. Inversion of Control is a two way process. First you have to bind implementations of interfaces or classes to the container and then resolve (one or multile times) the creation of instances.
Why using an Ioc container
An IoC Container simplifies the creation of instances. It resolves object dependencies and is optimized to create instances on first use. It also makes it possible to replace classes with other (mock) implementations by changing one line of code. And it works perfectly with constructor dependency injections, forcing you to write cleaner code. It can also replace "global" collection classes.
In short, you define how instances are to be created, not when.
Binding
There are two general ways to bind implementations to the container.
BindSingleton
If you bind using bindSingleton
the instance will be created on first use of Ioc::get(ShoppingCart::class)
.
All subsequent calls to the get
method will always return the same instance.
BindMultiple
This way any call of Ioc::get(Product::class)
will return a new instance of the Product
class.
Dependency Injection
A "Inversion of Control" container goes hand in hand with Constructor Dependency Injection.
Let's look at an example. Imagine we have a shopping cart, that depends of the current logged in customer:
If we bind the ShoppingCart
and Customer
class to the IoC container
and we resolve the ShoppingCart
class using
the container want to create a new shopping cart and see it requires a customer class.
For this reason, a Customer
instance is first created and passed as a parameter to the constructor of the ShoppingCart.
This nesting is recursive and takes into account “singletons” and “multiple” instances.
Of course, multiple constructor arguments are supported and cyclic dependencies are recognized when resolving via the get
method and will fire a Pyther\Ioc\Exceptions\ResolveException
exception.
More control
This library gives you a lot control how objects will be instanced.
More binding control
For example you can add your own constructor arguments:
As you can see, the default values for constructors are taken into account if no arguments are found.
Another way is to create a anonymous construct function:
This function is only executed later when the object is first instantiated.
This way you can also have function arguments:
This construct function can be also be a static method of another object:
Or as a method of an already instanced object:
Auch ein binding auf null ist legitim:
In diesem Fall wird beim Auflösen keine Exception ausgelöst, sondern auf null aufgelöst.
And finally you can also bind an already existing objects:
Although this is possible, it should be avoided. On the one hand, the creation of the instances should be left to the container, on the other hand this triggers the autoloader and executes code that may never be used.
More resolve control
For non singleton instances you can specifiy constructor arguments during the resolve phase:
Check if binding exists
To check if a binding exists, you can use the has
method:
Multiple Containers
If you have the rare case where you need more than one container, here we are.
The static methods of Ioc::bindSingleton(...)
, Ioc::bindMultiple(...)
and Ioc::get(...)
are suggar forms of
where Ioc::$default
is the default container. This way you can have separated multiple containers:
I'm sure you get it :)