Skip to content

MethodFactory

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

MethodFactory

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

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.

Methods

__construct

Constructs the MethodFactory.

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

Parameters:

Parameter Type Description
$class string the class name or container service ID on which the method is called
$method string the name of the method to invoke
$arguments mixed Optional arguments to pass to the method.

__invoke

Resolves the class and invokes the configured method with arguments.

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

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:

Parameter Type Description
$container \Psr\Container\ContainerInterface The container used to resolve the class and arguments

Return Value:

The result of invoking the method

Throws:

If the method does not exist

If the method is not public



Automatically generated on 2025-07-03

Clone this wiki locally