CallableFactory

Class
implements FactoryInterface
Read only: Yes Final: Yes

A factory that wraps a user-provided callable and executes it when invoked.

Description

The callable MUST accept a PSR-11 ContainerInterface as its first and only argument. This factory SHALL be used when the construction logic must be fully delegated to a closure.

This class allows dynamic resolution of services using the container context.

Table of Contents

Interfaces

FactoryInterface

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

Properties

 : Closure

Methods

__construct()

Constructs a CallableFactory instance.

 : mixed
__invoke()

Invokes the factory to create a service.

 : mixed
getArguments()

Retrieves the arguments for the callable from the container.

 : array<string|int, mixed>
Properties

$callable

Private
private Closure $callable

Description

The user-defined factory callable. This callable MUST accept a ContainerInterface and return a service instance.

Methods

__construct()

Public

Constructs a CallableFactory instance.

public __construct( callable  $callable) : mixed
Parameters
$callable : callable

Description

a callable that returns a service instance and accepts a container

__invoke()

Public

Invokes the factory to create a service.

public __invoke(ContainerInterface  $container) : mixed
Parameters
$container : ContainerInterface

Description

the PSR-11 container for dependency resolution

Return values

Description

the constructed service instance

getArguments()

Private

Retrieves the arguments for the callable from the container.

private getArguments(ContainerInterface  $container, ReflectionFunction  $function) : array<string|int, mixed>
Parameters
$container : ContainerInterface

Description

the PSR-11 container for dependency resolution

$function : ReflectionFunction

Description

the reflection function of the callable

Return values
array<string|int, mixed>

Description

the resolved arguments for the callable