Oihana PHP System

HydrateKey

Overrides the expected array key used to hydrate a property.

By default, Reflection::hydrate() maps array keys to property names. This attribute allows customizing that mapping, e.g., when the source data uses different naming conventions (e.g., snake_case vs camelCase), or when the key needs to be renamed for compatibility reasons.

Tags
example

Map an incoming key to a different property name

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

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

With optional nullable properties

class Product
{
    #[HydrateKey('product_id')]
    public ?int $id;
}
author

Marc Alcaraz

since
1.0.0
Attributes
#[Attribute]
\Attribute::TARGET_PROPERTY

Table of Contents

Properties

$key  : string

Methods

__construct()  : mixed

Properties

Methods

__construct()

public __construct(string $key) : mixed
Parameters
$key : string

        
On this page

Search results