Download the PHP package jshannon63/cobalt without Composer
On this page you can find all versions of the php package jshannon63/cobalt. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jshannon63/cobalt
More information about jshannon63/cobalt
Files in jshannon63/cobalt
Package cobalt
Short Description Autowired Dependency Injection Container for PHP with Dependency Caching
License MIT
Homepage https://github.com/jshannon63/container
Informations about the package cobalt
Cobalt - An Autowired Dependency Injection Container for PHP
Realized in fewer than 160 lines of source.
Well documented, perfect for building/learning.
100% PHPUnit test coverage
One of the fastest PHP dynamic autowired containers available
Cobalt was created to push the performance limits on what a PHP based dynamic autowired DI container can achieve. The Container::class implements the PSR-11 ContainerInterface and provides many of the features found in more notable container projects. Additionally, dependency caching capabilities make the Cobalt container a great choice for performance intensive applications. Cobalt and its simplistic code are perfect for learning or for use within projects or frameworks.
The Cobalt service container has the following features:
- Single class container implementing the PSR-11 ContainerInterface.
- ArrayAccess methods for container bindings.
- Constructor injection of type-hinted dependencies.
- Dependency injection through bind method closures.
- Autowired dependency resolution using Reflection.
- Top down inversion of control (IoC).
- Shared mode option (singleton only).
- Bind existing instances into the container.
- A self-binding global container instance.
Installation
Usage
Creating the container
Binding into the container
Binding does not instantiate the class. Instantiation is deferred until requested from the container. The bind method accepts 3 parameters... the abstract name, the concrete implementation name and a true or false for defining as a singleton. Notice in all three versions we use different abstract names. This is to show that the abstract name is free-form and is used as the "key" for array storage of bindings.
bind($abstract, $concrete=null, $singleton=false)
Resolving out of the container
$instance = resolve($abstract); (resolve checks for existing binding before instantiating)
Note: Trying to resolve will throw an exception if the requested binding does not exist.
Using the make()
method
The make method will bind()
then resolve()
to return a fully instantiated binding.
$instance = make($abstract, $concrete, $singleton=false)
Creating an alias to a binding
alias($alias, $binding)
Allows creating additional $id string keys for accessing existing container bindings.
Binding an existing instance
$instance = instance($abstract, $instance)
Note: The instance
method is deprecated but retained for backward compatibility. Instead, use bind($id, $instance)
to register an existing intance.
Checking if a binding exists
$bool = has($abstract)
Get the values of a single binding
$array = getBinding($abstract)
Getting a list of bindings
$array = getBindings()