This page explains the methods you can use on the Valvoid\Box\Box
object:
$box = new Box;
By default, Box
is shareable. The container provides the same instance
of itself to dependencies. To disable this behavior, pass false
to the
constructor:
$box = new Box(false);
In that case, the container will create a new instance each time a class
depends on Box
.
Inject Method
The inject
method sets default constructor arguments for a given class:
$box->inject(Any::class, parameter: argument);
These arguments are passed by name whenever the container creates an
instance and are used unless overridden by named arguments provided to
the get
method.
Recycle Method
The recycle
method marks one or more classes as shareable. The
container will instantiate each class only once and return the same
object whenever it is requested or injected.
$box->recycle(Any::class);
Map Method
The map
method connects a concrete implementation to an abstraction.
Whenever the abstraction (interface, abstract class, or parent class) is
requested or injected, the container will provide the mapped
implementation.
$box->map(Any::class, Abstraction::class);
Get Method
The get
method returns an instance of the given class and accepts named
arguments that override the defaults set by the inject
method
for the current resolution:
$box->get(Any::class, parameter: argument);
Note
On-demand arguments apply to all dependencies in the construction chain and are consumable, with the first matching parameter removed from the list.
Unset Method
The unset
method removes all settings for a given class, including
shareable references, default constructor arguments, and implementation
mappings.
$box->unset(Any::class);