Download the PHP package dc/ioc without Composer
On this page you can find all versions of the php package dc/ioc. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package ioc
Short Description Simple IoC container
License MIT
Homepage http://github.com/digitalcreations/ioc
Informations about the package ioc
Installation
Or add it to composer.json
:
Registering
You can register objects, classes and factory functions with the container.
Registering a class:
Registering an instance (instance registrations do not support lifetimes, they always live as long as the container does):
Registering a factory function:
Understanding lifetimes
There are 3 possible lifetime registrations: withPerResolveLifetime()
, withContainerLifetime()
and withSingletonLifetime()
. You can use these when you pass a class name or factory function to register()
, but not if you pass an instance of an object.
withPerResolveLifetime()
will create a new instance every time you resolve it.withContainerLifetime()
will resolve the same instance from that container.withSingletonLifetime()
will resolve the same instance from all containers.
Resolving services
You can resolve services by querying the container directly:
If you have multiple registrations for the same interface or class name, you can use resolveAll()
.
Constructor injection
Use type hints to inject a dependency into a class.
If you don't want to use type hints, you can use PhpDoc comments instead. This is particularly useful when you want to get an array of something, as this is not supported by PHPs type-hinting syntax.
Or even simpler:
Property injection
All objects resolved through the container will have their non-static properties scanned for possible dependencies. To mark a property as injectable, it needs both a @var
and a @inject
PhpDoc declaration:
For objects that haven't been resolved through the container (meaning objects you have constructed yourself), you can apply property injection using the inject()
method:
Factory injection
When you provide a factory function to registration, you can have other services injected as in a constructor. You can as before, either rely on type hints, or on PhpDoc comments (for injecting arrays).
Modules
If you have a large project that needs to register a lot of services, implementing a module may be the way to go.
Extend \DC\IoC\Modules\Module
and specify a name for your module and which modules it depends on.
When (preferably before) registering your services, register all the modules first:
The container will try to register any dependencies in the correct order.
Performance
Some crude performance tests can be found in the perf/
folder of this repos. They show the following (numbers from a VM running on my machine):
- A single registration uses about 600 bytes of memory, and takes 0.03 ms. Unless you are registering thousands of services, speed and memory should not be a limiting factor. For a typical setup with less than a hundred registrations per page view, you should expect it to add less than 25 ms to your bottom line in most cases.
- Resolves are negligible. A
resolveAll
-call that returns 10 objects, takes on average 0.09 ms (to resolve 10 objects). - Constructor and function injection is done using reflection, which is often assumed to be slow. Again, to resolve a single interface takes 0.06 ms.