Container parameters

Use constructor types to describe what a service needs. Register a binding when the container needs a choice from you, such as which implementation to use for an interface or how long to keep an object.

Register a binding

PHP
bind(string $abstract, mixed $concrete = null, bool $shared = false)
singleton(string $abstract, mixed $concrete = null)
$abstract · required
The lookup key. Use an interface or class name for constructor injection, or a string alias for explicit calls to make(). Registering the same key again replaces the binding and its cached result.
$concrete · default null
Omitting it uses the key itself as the class to construct. Supply an implementation class for an interface, or a factory that receives the container and the current parameter array. A regular, non-invokable object passed to singleton() becomes the stored instance immediately. Passing it to a transient binding uses its class for fresh construction.
$shared · default false
By default each lookup constructs the class or calls the factory again. With true, the first successful result is cached under that key. Later calls return it even if they supply different parameters. singleton() is the shorthand for this setting.

Resolve constructor arguments

PHP
make(string $abstract, ?array $params = [])

For an unbound concrete class, the container inspects its constructor. For a bound implementation class it uses the same rules. For each parameter, resolution stops at the first match below.

  1. Explicit name. A key in $params matching the constructor parameter name supplies its value, including an explicit null for a nullable parameter. Omit the dollar sign in the key. Overrides apply to this construction. Nested dependencies start with their own empty parameter array.
  2. Registered type. A binding for the parameter’s single named type supplies the value. This includes primitive type bindings.
  3. Declared default. If the constructor supplies a default, the container uses it.
  4. Concrete type. The container recursively constructs a named class. An interface or abstract class needs a binding to an instantiable implementation or factory.

A required untyped, union or intersection parameter needs an explicit value. A required built-in type needs a value or a suitable binding. For circular dependencies, the exception includes the chain of lookup keys. Passing null as the entire parameter array is equivalent to passing an empty array.

Try the precedence rules

Save this script in the project root and run it with PHP. The same constructor produces three different values as you introduce a type binding and then an override.

PHP
<?php

require __DIR__.'/vendor/autoload.php';

final class Batch
{
    public function __construct(public int $size = 10) {}
}

$app = new \M4W\Core\Framework();
echo $app->make(Batch::class)->size, PHP_EOL;                 // 10
$app->bind('int', 20);
echo $app->make(Batch::class)->size, PHP_EOL;                 // 20
echo $app->make(Batch::class, ['size' => 5])->size, PHP_EOL;  // 5

$app->singleton(Batch::class);
$first = $app->make(Batch::class, ['size' => 8]);
$next = $app->make(Batch::class, ['size' => 2]);
var_dump($first === $next, $next->size);                      // true, 8

Primitive bindings

The reserved keys are string, int, float, bool and array. Binding one affects every constructor parameter of that type in the container unless the caller supplies a named override. Use a named override when only one service needs a value.

Supply a literal, a closure or an invokable object. Primitive factories run on each lookup. A callable-looking string such as 'strlen' bound to string stays a literal. The resolved value must match the key’s type exactly. For example, binding '3' to int fails on lookup. Registering a primitive as shared throws immediately.

Container scope

Each application resolves 'app', Container::class and Framework::class to itself. containerHas($key) checks for an explicit binding, so it can return false for a concrete class that make() can construct. isShared($key) returns the registered lifetime and throws for a missing key.

The skeleton passes its container explicitly. For code using app() or a facade, first select the application with Framework::setInstance($app). Then app($key, $parameters) delegates to that container’s make(). Call Framework::resetInstance() to clear the selection. Selecting another application switches the target of global calls while each application retains its own bindings.