Created
April 22, 2024 13:24
-
-
Save n1c/c93f2169bff059c94cd49bbb596021be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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