MethodFactory

Class
implements FactoryInterface
Read only: Yes Final: Yes

A factory that invokes a specified method on a class using reflection and the PSR-11 container.

Description

This factory MUST be used when service creation requires calling a non-constructor method, and supports both static and instance methods.

If the method is not public, a RuntimeException SHALL be thrown.

Arguments MAY be resolved from the container if passed as service identifiers.

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

Methods

__construct()

Constructs the MethodFactory.

 : mixed
__invoke()

Resolves the class and invokes the configured method with arguments.

 : mixed
Properties

$arguments

Private
private array<int, mixed> $arguments

Description

arguments to be passed to the method during invocation

Methods

__construct()

Public

Constructs the MethodFactory.

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

Description

the class name or container service ID on which the method is called

$method : string

Description

the name of the method to invoke

$arguments : mixed

Description

Optional arguments to pass to the method.

__invoke()

Public

Resolves the class and invokes the configured method with arguments.

public __invoke(ContainerInterface  $container) : mixed

Description

Arguments MAY be resolved from the container if passed as string identifiers and found. Static methods are invoked without instantiating the class. If the method is not public, this method MUST throw a RuntimeException.

Parameters
$container : ContainerInterface

Description

The container used to resolve the class and arguments

Tags
throws
ReflectionException

Description

If the method does not exist

throws

Description

If the method is not public

Return values

Description

The result of invoking the method