Attributable
Raxos\Reflection\Attributable
The trait shared by every reflector in the package. It adds consistent PHP attribute reading to ClassReflector, MethodReflector, PropertyReflector, ParameterReflector and FunctionReflector, so attribute access works the same way everywhere.
trait AttributableThe trait holds the underlying native reflection object in a shared $reflection property, which is the object it reads attributes from.
Methods
getAttribute()
public function getAttribute(string $name, bool $recursive = false): ?objectReturns an instantiated attribute, or null when it is absent. On a ClassReflector, passing recursive: true continues the search up the implemented interfaces and then the parent class chain.
getAttributes()
public function getAttributes(string $name): arrayReturns all matching attribute instances, including subclasses of the given attribute.
getRawAttribute()
public function getRawAttribute(string $name): ?ReflectionAttributeReturns the first matching attribute as a native ReflectionAttribute, without instantiating it.
getRawAttributes()
public function getRawAttributes(string $name): arrayReturns all matching attributes as native ReflectionAttribute objects, without instantiating them.
hasAttribute()
public function hasAttribute(string $name, bool $instanceOf = false): boolChecks whether the given attribute is present without instantiating it. Pass instanceOf: true to also match subclasses.
Usage
<?php
declare(strict_types=1);
use function Raxos\Reflection\reflect;
$reflector = reflect(User::class);
$table = $reflector->getAttribute(Table::class);
if ($reflector->hasAttribute(Table::class)) {
// ...
}
foreach ($reflector->getRawAttributes(Column::class) as $attribute) {
// $attribute->getArguments()
}See the Reading attributes concept page for a fuller walkthrough.