FunctionReflector
Raxos\Reflection\FunctionReflector
Reflects a standalone function or a closure. It exposes the function name, its file location and its parameters, and it can invoke the function with the given arguments. It uses the Attributable trait for attribute reading.
final readonly class FunctionReflector implements ReflectorInterfaceThe class implements the ReflectorInterface contract from raxos/contract.
Methods
__construct()
public function __construct(ReflectionFunction|Closure $function)Creates a reflector from a Closure or a native ReflectionFunction.
getName()
public function getName(): stringReturns the function name.
getShortName()
public function getShortName(): stringReturns the short name of the function.
getFileName()
public function getFileName(): stringReturns the file the function is defined in.
getStartLine()
public function getStartLine(): intReturns the line the function declaration starts on.
getEndLine()
public function getEndLine(): intReturns the line the function declaration ends on.
invokeArgs()
public function invokeArgs(array $args = []): mixedInvokes the function with the given arguments.
getParameters()
public function getParameters(): GeneratorYields a ParameterReflector for each parameter.
getParameter()
public function getParameter(int|string $key): ?ParameterReflectorReturns a reflector for a parameter by name or position, or null when it does not exist.
Usage
<?php
declare(strict_types=1);
use Raxos\Reflection\FunctionReflector;
$reflector = new FunctionReflector(static fn(int $value): int => $value * 2);
foreach ($reflector->getParameters() as $parameter) {
echo $parameter->getName() . "\n";
}
echo $reflector->invokeArgs([21]); // 42