ClassReflector
Raxos\Reflection\ClassReflector
The entry point for reflecting a class. It gives typed access to a class name, its short name and file, its properties, methods and constructor, its parent and implemented interfaces, and it can create instances and call static methods. It uses the Attributable trait for attribute reading.
final readonly class ClassReflector implements ReflectorInterfaceThe class implements the ReflectorInterface contract from raxos/contract.
Methods
__construct()
public function __construct(string|object $class)Creates a reflector from a class name, an object instance, another ClassReflector or a ReflectionClass. The global reflect() helper is a convenient shortcut for the string or ReflectionClass case.
getName()
public function getName(): stringReturns the fully qualified class name.
getShortName()
public function getShortName(): stringReturns the class name without its namespace.
getFileName()
public function getFileName(): string|falseReturns the file the class is defined in, or false for internal classes.
getInterfaces()
public function getInterfaces(): GeneratorYields a TypeReflector for each implemented interface.
getParent()
public function getParent(): ?selfReturns a ClassReflector for the parent class, or null when there is none.
getType()
public function getType(): TypeReflectorReturns a TypeReflector representing this class.
getPublicProperties()
public function getPublicProperties(): GeneratorYields a PropertyReflector for each public property.
getProperties()
public function getProperties(): GeneratorYields a PropertyReflector for every property regardless of visibility.
getProperty()
public function getProperty(string $name): ?PropertyReflectorReturns a reflector for a single named property.
hasProperty()
public function hasProperty(string $name): boolChecks whether the class declares the given property.
getConstructor()
public function getConstructor(): ?MethodReflectorReturns a MethodReflector for the constructor, or null if there is none.
getPublicMethods()
public function getPublicMethods(): GeneratorYields a MethodReflector for each public method.
getMethods()
public function getMethods(): GeneratorYields a MethodReflector for every method regardless of visibility.
getMethod()
public function getMethod(string $name): ?MethodReflectorReturns a reflector for a single named method.
isInstantiable()
public function isInstantiable(): boolChecks whether the class can be instantiated.
newInstanceArgs()
public function newInstanceArgs(array $args = []): objectCreates a new instance, passing the given constructor arguments.
newInstanceWithoutConstructor()
public function newInstanceWithoutConstructor(): objectCreates a new instance without invoking the constructor.
callStatic()
public function callStatic(string $method, mixed ...$args): mixedCalls a static method on the class with the given arguments.
implements()
public function implements(string $interface): boolChecks whether the class implements the given interface.
is()
public function is(string $type): boolChecks whether the class matches the given class or interface name.
Usage
<?php
declare(strict_types=1);
use function Raxos\Reflection\reflect;
$reflector = reflect(User::class);
if ($reflector->isInstantiable()) {
$user = $reflector->newInstanceArgs(['Bas']);
}
foreach ($reflector->getPublicMethods() as $method) {
echo $method->getShortName() . "\n";
}