ParameterReflector
Raxos\Reflection\ParameterReflector
Reflects a single parameter of a function or method. It exposes the declaring class or function, the parameter type, its position and default value, and a set of predicates that describe how the parameter can be called. It uses the Attributable trait for attribute reading.
final readonly class ParameterReflector implements ReflectorInterfaceThe class implements the ReflectorInterface contract from raxos/contract.
Methods
__construct()
public function __construct(ReflectionParameter $parameter)Creates a reflector from a native ReflectionParameter. You usually obtain one from a MethodReflector or FunctionReflector.
getClass()
public function getClass(): ?ClassReflectorReturns a ClassReflector for the declaring class, or null when the parameter belongs to a standalone function.
getFunction()
public function getFunction(): FunctionReflector|MethodReflectorReturns a reflector for the declaring function or method.
getDefaultValue()
public function getDefaultValue(): mixedReturns the default value of the parameter.
getName()
public function getName(): stringReturns the parameter name.
getPosition()
public function getPosition(): intReturns the zero based position of the parameter.
getType()
public function getType(): TypeReflectorReturns a TypeReflector for the parameter's declared type.
hasDefaultValue()
public function hasDefaultValue(): boolChecks whether the parameter has a default value.
hasType()
public function hasType(): boolChecks whether the parameter has a declared type.
isIterable()
public function isIterable(): boolChecks whether the parameter's type is iterable.
isNullable()
public function isNullable(): boolChecks whether the parameter's type allows null.
isOptional()
public function isOptional(): boolChecks whether the parameter is optional.
isRequired()
public function isRequired(): boolChecks whether the parameter must always be provided. A parameter is required when its type does not allow null and it is not optional.
isVariadic()
public function isVariadic(): boolChecks whether the parameter is variadic.
Usage
<?php
declare(strict_types=1);
use function Raxos\Reflection\reflect;
$method = reflect(User::class)->getConstructor();
foreach ($method->getParameters() as $parameter) {
echo $parameter->getPosition() . ': ' . $parameter->getName();
echo $parameter->isRequired() ? ' (required)' : ' (optional)';
echo "\n";
}