Download the PHP package yuloh/container without Composer
On this page you can find all versions of the php package yuloh/container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download yuloh/container
More information about yuloh/container
Files in yuloh/container
Package container
Short Description A lightweight container-interop compatible DI container
License MIT
Homepage https://github.com/yuloh/container
Informations about the package container
Container
Container is a lightweight dependency injection container. It's compatible with the PSR-11 container interface, so you can use it with lots of different projects out of the box.
The container is really simple; It's basically an array of identifier => callable mappings, and the callable is invoked to get the resulting object.
New to dependency injection and containers? I wrote a blog post explaining dependency injection and how this container works.
This package is compliant with PSR-1, PSR-2, PSR-4, and PSR-11.
Install
Via Composer
Usage
Adding Entries
Adding an entry to the container is really simple. Just specify the identifier as the first argument, and a callable as the second argument.
The closure will receive the container as it's only argument, so you can use the container to resolve the dependencies of your entry.
All entries are shared (singletons), which means an entry will be resolved once and reused for subsequent calls.
Getting Entries
To check if an entry exists, use has
. To get an entry, use get
. If you are just retrieving entries you can typehint Psr\Container\ContainerInterface
instead of the actual Container.
Why Another Container?
There are a lot of containers out there. I was working on a project and wanted a lightweight default container and couldn't find what I wanted. This container:
- Implements container-interop.
- Supports PHP 5.4+
- Supports adding entries at runtime.
- Is incredibly lightweight, with the bare minimum of code to support the first 3 goals.