1. Go to this page and download the library: Download oihana/php-reflect library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
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);
use oihana\reflect\attributes\{HydrateKey, HydrateWith, HydrateAs};
class WithKey { #[HydrateKey('user_name')] public string $name; }
// Maps input key 'user_name' to property 'name'
class Geo { #[HydrateWith(Address::class)] public array $locations = []; }
// Hydrates each element of an array property as Address
class Wrapper { #[HydrateAs(Address::class)] public object $payload; }
// Overrides ambiguous type (object/array/mixed/union)
class Address { public string $city; }
class Geo { /** @var Address[] */ public array $locations = []; }
$geo = (new Reflection())->hydrate(
['locations' => [ ['city' => 'Lyon'], ['city' => 'Nice'] ]],
Geo::class
);
class A { public string $type = 'A'; }
class B { public string $type = 'B'; }
class Box { #[HydrateWith(A::class, B::class)] public array $items = []; }
// Chooses the right class using '@type' or 'type', or best-guess by properties
use oihana\reflect\traits\ReflectionTrait;
class Product {
use ReflectionTrait;
public string $name = 'Book';
public ?string $desc = null;
}
$p = new Product();
$data = $p->jsonSerializeFromPublicProperties(Product::class, true); // ['name' => 'Book']
use oihana\reflect\traits\ConstantsTrait;
final class Status { use ConstantsTrait; public const string OPEN = 'open'; public const string CLOSED = 'closed'; }
Status::
use oihana\reflect\Version;
$v = new Version(1, 2, 3, 4);
$v->fields = 3; // print as 1.2.3
echo (string) $v; // "1.2.3"
$v->major = 2; // mutate safely
$n = $v->valueOf(); // packed 32-bit int
bash
composer
bash
composer test ./tests/oihana/reflect/VersionTest.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.