Oihana PHP System

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
example
echo (new Reflection())->className(new \App\Entity\User());
// Output: 'User'
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
throws
ReflectionException

If reflection fails.

example
class MyStatus {
    public const ACTIVE = 'active';
    private const SECRET = 'hidden';
}

$constants = (new Reflection())->constants(MyStatus::class);
print_r($constants);
// Output: ['ACTIVE' => 'active']
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
throws
InvalidArgumentException

If the class does not exist or required non-nullable property is null.

throws
ReflectionException

If property introspection fails.

example

Flat object hydration

class User {
    public string $name;
}

$data = ['name' => 'Alice'];
$user = (new Reflection())->hydrate($data, User::class);
echo $user->name; // "Alice"
example

Nested object hydration

class Address {
    public string $city;
}
class User {
    public string $name;
    public ?Address $address = null;
}

$data = ['name' => 'Alice', 'address' => ['city' => 'Paris']];
$user = (new Reflection())->hydrate($data, User::class);
echo $user->address->city; // "Paris"
example

Hydration with #[HydrateKey]

use oihana\reflections\attributes\HydrateKey;

class User {
    #[HydrateKey('user_name')]
    public string $name;
}

$data = ['user_name' => 'Bob'];
$user = (new Reflection())->hydrate($data, User::class);
echo $user->name; // "Bob"
example

Hydration of array of objects via #[HydrateWith]

use oihana\reflections\attributes\HydrateWith;

class Address {
    public string $city;
}
class Geo {
    #[HydrateWith(Address::class)]
    public array $locations = [];
}

$data = ['locations' => [['city' => 'Paris'], ['city' => 'Berlin']]];
$geo = (new Reflection())->hydrate($data, Geo::class);
echo $geo->locations[1]->city; // "Berlin"
example

Hydration of array via @var Type[]

class Address
{
    public string $city;
}

class Geo
{
    / ** @ var Address[] * /
    public array $locations = [];
}

$data = ['locations' => [['city' => 'Lyon'], ['city' => 'Nice']]];
$geo = (new Reflection())->hydrate($data, Geo::class);
echo $geo->locations[0]->city; // "Lyon"
example

Hydration of array via @var array<Address>

class Address
{
    public string $city;
}

class Geo
{
    / ** @ var array<Address> * /
    public array $locations = [];
}
example

Union types

class Profile {
    public ?string $bio = null;
}

$data = ['bio' => null];
$profile = ( new Reflection() )->hydrate( $data , Profile::class ) ;
var_dump($profile->bio); // null
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
throws
ReflectionException

If reflection fails.

example
class MyClass
{
    public function foo() }
    protected function bar() }
}

$methods = (new Reflection())->methods(MyClass::class);
foreach ($methods as $method)
{
    echo $method->getName(); // 'foo'
}
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
throws
ReflectionException

If reflection fails.

example
class Item {
    public string $name;
    private int $id;
}
$props = (new Reflection())->properties(Item::class);
foreach ($props as $prop) {
    echo $prop->getName(); // 'name'
}
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
throws
ReflectionException

If reflection fails.

example
$reflectionClass = (new Reflection())->reflection(\App\Entity\User::class);
echo $reflectionClass->getName(); // 'App\Entity\User'
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
throws
ReflectionException

If reflection fails.

example
echo (new Reflection())->shortName(\App\Models\Product::class);
// Output: 'Product'
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
throws
ReflectionException
Return values
string|null

guessClassFromProperties()

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
throws
ReflectionException

If a class cannot be reflected upon.

example
class User
{
    #[HydrateKey('user_id')]
    public string $id;
    public string $name;
}

class Product
{
    public string $sku;
    public string $name;
}

$item = ['user_id' => '123', 'name' => 'Alice'];
$guessedClass = $this->guessClassFromProperties($item, [User::class, Product::class]);

echo $guessedClass; // Outputs: "User"
Return values
string|null

The best matching class name or null if none found.


        
On this page

Search results