Reflection
Table of Contents
Properties
- $reflections : array<string, ReflectionClass>
- Internal cache of reflection instances.
Methods
- className() : string
- Returns the short class name (without namespace) of the given object.
- constants() : array<string, mixed>
- Returns an array of constants defined in the given class.
- hydrate() : object
- Instantiates and hydrates an object of a given class using associative array data.
- methods() : array<int, ReflectionMethod>
- Returns an array of methods for the given class or object.
- properties() : array<int, ReflectionProperty>
- Returns an array of properties for the given class or object.
- reflection() : ReflectionClass
- Returns a cached ReflectionClass instance for the given class or object.
- shortName() : string
- Returns the short (unqualified) name of the class.
- determineArrayItemType() : string|null
- Determine the type of an array element
- guessClassFromProperties() : string|null
- Attempts to guess the most appropriate class from a list of possible classes based on the presence of matching properties in the provided input array.
Properties
$reflections
Internal cache of reflection instances.
protected
array<string, ReflectionClass>
$reflections
= []
Methods
className()
Returns the short class name (without namespace) of the given object.
public
className(object $object) : string
Parameters
- $object : object
-
The object to reflect.
Tags
Return values
string —The short class name.
constants()
Returns an array of constants defined in the given class.
public
constants(object|string $class[, int $filter = ReflectionClassConstant::IS_PUBLIC ]) : array<string, mixed>
Parameters
- $class : object|string
-
The object or class name.
- $filter : int = ReflectionClassConstant::IS_PUBLIC
-
A bitmask of constant visibility (default: public).
Tags
Return values
array<string, mixed> —Associative array of constant names and values.
hydrate()
Instantiates and hydrates an object of a given class using associative array data.
public
hydrate(array<string|int, mixed> $thing, string $class) : object
It supports:
- Recursive hydration for nested objects (flat or array),
- Union types (e.g.,
Type|null
), - Custom source keys with
#[HydrateKey]
, - Array hydration via
#[HydrateWith]
,#[HydrateAs]
, or PHPDoc@ var Type[]
, - Public properties only (private/protected are ignored).
Parameters
- $thing : array<string|int, mixed>
-
Associative array of data (keys must match public properties or be aliased via attributes).
- $class : string
-
Fully qualified class name of the object to instantiate.
Tags
Return values
object —The hydrated object instance.
methods()
Returns an array of methods for the given class or object.
public
methods(object|string $class[, int $filter = ReflectionMethod::IS_PUBLIC ]) : array<int, ReflectionMethod>
Parameters
- $class : object|string
-
The object or class name.
- $filter : int = ReflectionMethod::IS_PUBLIC
-
Method visibility filter (default: public).
Tags
Return values
array<int, ReflectionMethod> —Array of reflection method objects.
properties()
Returns an array of properties for the given class or object.
public
properties(object|string $class[, int $filter = ReflectionProperty::IS_PUBLIC ]) : array<int, ReflectionProperty>
Parameters
- $class : object|string
-
The object or class name.
- $filter : int = ReflectionProperty::IS_PUBLIC
-
Property visibility filter (default: public).
Tags
Return values
array<int, ReflectionProperty> —Array of reflection property objects.
reflection()
Returns a cached ReflectionClass instance for the given class or object.
public
reflection(object|string $class) : ReflectionClass
Parameters
- $class : object|string
-
The object or class name.
Tags
Return values
ReflectionClass —The reflection class.
shortName()
Returns the short (unqualified) name of the class.
public
shortName(object|string $class) : string
Parameters
- $class : object|string
-
The object or class name.
Tags
Return values
string —The short name of the class.
determineArrayItemType()
Determine the type of an array element
private
determineArrayItemType(mixed $item, array<string|int, mixed> $possibleClasses) : string|null
Parameters
- $item : mixed
- $possibleClasses : array<string|int, mixed>
Tags
Return values
string|nullguessClassFromProperties()
Attempts to guess the most appropriate class from a list of possible classes based on the presence of matching properties in the provided input array.
private
guessClassFromProperties(array<string|int, mixed> $item, array<string|int, mixed> $possibleClasses) : string|null
The score is computed by checking if each class property (or its alternative
key defined by a HydrateKey
attribute) exists in the $item
array. The class
with the highest normalized score (above 0.3) is returned.
If no class scores high enough, the first class in the $possibleClasses
list is
returned as fallback (if provided), otherwise null
.
Parameters
- $item : array<string|int, mixed>
-
The associative array of input data to match against class properties.
- $possibleClasses : array<string|int, mixed>
-
A list of fully qualified class names to consider.
Tags
Return values
string|null —The best matching class name or null
if none found.