MethodReflector
Raxos\Reflection\MethodReflector
Reflects a single class method: its parameters, return type and declaring class, and it can invoke the method on an instance. It uses the Attributable trait for attribute reading and implements SerializableInterface, so a method reference can be serialized and restored.
final readonly class MethodReflector implements ReflectorInterface, SerializableInterfaceBoth ReflectorInterface and SerializableInterface come from raxos/contract.
Methods
__construct()
public function __construct(ReflectionMethod $method)Creates a reflector from a native ReflectionMethod. You usually obtain one from a ClassReflector instead of constructing it directly.
getParameters()
public function getParameters(): GeneratorYields a ParameterReflector for each parameter of the method.
getParameter()
public function getParameter(string $name): ?ParameterReflectorReturns a reflector for a single named parameter, or null when it does not exist.
getClass()
public function getClass(): ClassReflectorReturns a ClassReflector for the declaring class.
getReturnType()
public function getReturnType(): ?TypeReflectorReturns a TypeReflector for the declared return type, or null when the method has none.
getName()
public function getName(): stringReturns the method name.
getShortName()
public function getShortName(): stringReturns a readable signature such as User::rename(string $name).
invokeArgs()
public function invokeArgs(?object $instance, array $args = []): mixedInvokes the method on the given instance with the given arguments. Pass null as the instance for static methods.
Serialization
MethodReflector implements __serialize() and __unserialize(). It serializes to the declaring class name and method name, and restores itself by constructing a fresh ReflectionMethod from that pair. This makes it safe to store a method reference and rebuild it later.
Usage
<?php
declare(strict_types=1);
use function Raxos\Reflection\reflect;
$method = reflect(User::class)->getMethod('rename');
$result = $method->invokeArgs($user, ['Bas']);