PHP code example of gravatalonga / hydrator

1. Go to this page and download the library: Download gravatalonga/hydrator 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/ */

    

gravatalonga / hydrator example snippets

 php
$properties = ['id' => 1, 'string' => 'hello world']

class Entity implements HydrableInterface
{
    use HydrateTrait;
    
    public int $id;
    
    public string $string;
}

$e = Entity::hydrate($property);
echo $e->id; // 1
echo $e->string; // hello world
 php
$properties = ['name' => 'Jonathan']

class Entity implements HydrableInterface
{
    use HydrateTrait;
    
    public string $name;
    
    private function formatName($value)
    {
        return 'Hi, ' . $value;
    }
}

$e = Entity::hydrate($property);
echo $e->string; // Hi, Jonathan  

$hydrator = new Hydrator(['id' => 1]);
$entity = $hydrator->hydrate(new Entity);

echo $entity->id; // 1