Download the PHP package nimbly/carton without Composer
On this page you can find all versions of the php package nimbly/carton. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nimbly/carton
More information about nimbly/carton
Files in nimbly/carton
Package carton
Short Description A simple PSR-11 container implementation offering reflection based autowiring.
License MIT
Informations about the package carton
Carton
A simple PSR-11 container implementation.
Features
- PSR-11 compliant
- Singleton and factory builders
- Service providers
- Nested container support
- Reflection based autowiring
Install
Getting the container
Instantiate container
You can create a new instance of a Container.
Singleton instance
Or use the container singleton getInstance
method.
Basic usage
Set an instance
The most basic usage is to just assign an instance to the container itself.
Of course, you don't need to assign objects - it can be anything you like.
Retrieving a value
Grab a value from the container by its key.
NOTE: Retrieving a value that does not exist will throw a NotFoundException
.
Checking for instance
You can check for the existance of items registered in the container.
Advanced usage
Singleton builder
The singleton builder will ensure only a single instance is ever returned when it is retrieved from the container.
It also has the added benefit over the set
method by lazily instantiating the class. I.e. it will only be created when it is actually needed.
Factory builder
The factory builder will create new instances each time it is retrieved from the container.
Autowiring
You can have instances made for you automatically using the make
method - which will attempt to pull dependencies in from the container itself or recursively attempt to make
them if not found.
Dependecy injection on instance methods
Calling an instance method couldn't be easier - Carton will attempt to autoresolve dependencies (autowire) for you when making a call to an instance method.
Adding additional containers
You can extend Carton with additional PSR-11 compliant container instances by calling the addContainer
method. When Carton attempts to resolve an item, it will
always attempt to resolve locally first, and if not found, will loop through any additional containers you have provided.
For example, if you had a configuration manager that implemented ContainerInterface
(PSR-11),
you could add it to Carton.
Now you can retrieve your configuration data through the container instance.
Service providers
Service providers allow you to organize your application dependencies in a set of classes.
Create service classes that implement ServiceProviderInterface
.
Then register your service providers with the container.
You can also register multiple services at once and register services by their class name.