Skip to documentation

MethodFactory

Class
implements FactoryInterface
Read only: Yes Final: Yes

A factory that invokes a public method on a class or a container-provided object.

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

Methods

__construct()

Constructs the MethodFactory.

 : mixed
__invoke()

Resolves the class and invokes the configured method with arguments.

 : mixed
 : ReflectionMethod|null
 : ReflectionMethod
 : array<int|string, mixed>
Properties

$arguments

Private
private array<int|string, mixed> $arguments

Description

positional or named arguments passed to the method

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. Public static class methods are invoked without resolving an instance. Instance targets are retrieved by class name or service ID and reflected using the resolved object. Direct construction is permitted only when has() reports a class-name entry as absent. Failures resolving registered targets propagate, including missing nested dependencies.

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

throws
NotFoundExceptionInterface

Description

if a non-class service ID is absent

Return values

Description

The result of invoking the method

findPublicStaticMethod()

Private
private findPublicStaticMethod() : ReflectionMethod|null
Return values
ReflectionMethod|null

Description

a public static method that needs no target instance

reflectPublicMethod()

Private
private reflectPublicMethod( object|class-string  $target) : ReflectionMethod
Parameters
$target : object|class-string

Description

the resolved object or a static method's class

Tags
throws
ReflectionException

Description

if the method does not exist

throws

Description

if the method is not public

Return values
ReflectionMethod

Description

the public method to invoke

resolveArguments()

Private
private resolveArguments(ContainerInterface  $container) : array<int|string, mixed>
Parameters
$container : ContainerInterface

Description

the source of named method arguments

Return values
array<int|string, mixed>

Description

resolved arguments with their positional or named keys

resolveObject()

Private
private resolveObject(ContainerInterface  $container) : object
Parameters
$container : ContainerInterface

Description

the source of the target instance

Tags
throws
NotFoundExceptionInterface

Description

if a non-class service ID is absent

Return values
object

Description

the configured instance, or a class with no registered entry

Search results