Skip to content

InvokableFactory

Felipe Sayão Lobato Abreu edited this page Jul 3, 2025 · 2 revisions

InvokableFactory

Factory responsible for instantiating a class with constructor arguments.

This class MUST be used when a service should be created using a known class name and optionally injected with fixed arguments.

It SHALL invoke the constructor directly using the spread operator and provided arguments. This factory is suitable for services that do not require container-based dependency injection.

Methods

__construct

Constructs the InvokableFactory with a target class and optional constructor arguments.

public __construct(string $class, mixed $arguments): mixed

This constructor MUST receive a valid, instantiable class name. Any variadic arguments provided SHALL be passed to the class constructor during instantiation. If an argument is a string and matches a service ID in the container, it SHALL be resolved from the container.

Parameters:

Parameter Type Description
$class string the fully qualified class name to be instantiated
$arguments mixed A variadic list of constructor arguments.

__invoke

Creates an instance of the class with the provided arguments.

public __invoke(\Psr\Container\ContainerInterface $container): mixed

Arguments that are strings and match a known service ID in the container SHALL be replaced with the corresponding container-resolved services.

It MUST return a new instance of the class with the resolved arguments. This method SHALL be invoked by the container when the service is requested.

Parameters:

Parameter Type Description
$container \Psr\Container\ContainerInterface The PSR-11 compliant container providing dependencies

Return Value:

The created service instance

Throws:

thrown if an error occurs during service instantiation

thrown if a required service ID is not found in the container



Automatically generated on 2025-07-03

Clone this wiki locally