Download the PHP package gdetassigny/simple-container without Composer
On this page you can find all versions of the php package gdetassigny/simple-container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download gdetassigny/simple-container
More information about gdetassigny/simple-container
Files in gdetassigny/simple-container
Package simple-container
Short Description A simple implementation of the standard container interface, with support for autowiring
License MIT
Informations about the package simple-container
Simple Container
This is a simple implementation of the standard container interface, with support for autowiring.
This means that this package will attempt to figure out all your basic dependencies for you. It also supports manually defining dependencies in a YAML file, or even adding service providers for more complex dependencies.
Installation
This package requires at least PHP 7.2.
Install it using composer:
How to use it
Basic usage
Setting up a basic container is quite simple:
That's it! You can now request your dependencies from the container, thanks to autowiring.
Autowiring
The container will be able to instantiate simple services which have straightforward dependencies in their constructors:
The below examples can all be autowired:
Some cases cannot be autowired:
YAML Config
Simple container also supports adding a YAML config to manually define dependencies.
This can be useful to define a concrete class implementation of an interface. Or you may want to define a different name for your service than its class name.
Simply pass the path of the YAML file to the container provider:
Service Provider
If a service requires a more complex setup, you may want to look at using a service provider.
Ordering
When called, the container will look for your service using all 3 above options in that order:
- service provider
- YAML config
- autowiring
Why use this instead of another container package?
To be honest? Maybe you shouldn't. I built this mostly out of interest to understand how containers work under the hood. There are a lot of more evolved PHP containers out there.
However, I tried to make this package as straightforward to use as possible. If having a container up and running very quickly appeals to you then feel free to give this a go!
Furthermore, as it respects the standard container interface, you can always try it for a bit and later switch to another container package without too much hassle.