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.
Interfaces
Defines a contract for service factories that rely on a PSR-11 container for instantiation.
Properties
Methods
Constructs the MethodFactory.
Resolves the class and invokes the configured method with arguments.
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.
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
Description
If the method does not exist
Description
if a non-class service ID is absent
Return values
Description
The result of invoking the method
private
findPublicStaticMethod() : ReflectionMethod|null
Return values
Description
a public static method that needs no target instance
private
reflectPublicMethod(
object|class-string
$target) : ReflectionMethod
Parameters
$target
:
object|class-string
Description
the resolved object or a static method's class
Description
if the method does not exist
Return values
Description
the public method to invoke
private
resolveArguments(ContainerInterface
$container) : array<int|string, mixed>
Parameters
$container
:
ContainerInterface
Description
the source of named method arguments
Return values
Description
resolved arguments with their positional or named keys
private
resolveObject(ContainerInterface
$container) : object
Parameters
$container
:
ContainerInterface
Description
the source of the target instance
Description
if a non-class service ID is absent
Return values
Description
the configured instance, or a class with no registered entry