InvokableFactory

Class
implements FactoryInterface
Read only: Yes Final: Yes

Factory responsible for instantiating a class with constructor arguments.

Description

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.

Table of Contents

Interfaces

FactoryInterface

Defines a contract for service factories that rely on a PSR-11 container for instantiation.

Properties

 : array<int, mixed>
 : string

Methods

__construct()

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

 : mixed
__invoke()

Creates an instance of the class with the provided arguments.

 : mixed
Properties

$arguments

Private
private array<int, mixed> $arguments

Description

The list of arguments to pass to the class constructor. This MAY be empty if the constructor takes no arguments.

Methods

__construct()

Public

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

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

Description

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
$class : string

Description

the fully qualified class name to be instantiated

$arguments : mixed

Description

A variadic list of constructor arguments.

__invoke()

Public

Creates an instance of the class with the provided arguments.

public __invoke(ContainerInterface  $container) : mixed

Description

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
$container : ContainerInterface

Description

The PSR-11 compliant container providing dependencies

Tags
throws
ContainerExceptionInterface

Description

thrown if an error occurs during service instantiation

throws
NotFoundExceptionInterface

Description

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

Return values

Description

The created service instance