Skip to content

Instantly share code, notes, and snippets.

@n1c
Created April 22, 2024 13:24
Show Gist options
  • Save n1c/c93f2169bff059c94cd49bbb596021be to your computer and use it in GitHub Desktop.
Save n1c/c93f2169bff059c94cd49bbb596021be to your computer and use it in GitHub Desktop.
<?php
trait Inflates
{
public function __construct(object $data)
{
$vars = get_class_vars(self::class);
foreach ($vars as $key => $value) {
$this->$key = $data->$key;
}
}
}
class Person
{
use Inflates;
public string $name;
public int $age;
public Position $position;
}
class Position
{
public int $lat;
public int $lng;
}
$data = (object) [
'name' => 'James',
'age' => 24,
'position' => (object) [
'lat' => 123,
'lng' => 456,
],
];
$person = new Person($data);
var_dump($person);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment