- property_exists: documentation ( source)
- var_dump: documentation ( source)
<?php
$config = ['legendaryName' => 'legendary_name'];
$data = ['legendary_name' => 'The Hammer'];
class Warrior {
use DbHydrate;
private $legendaryName;
}
trait DbHydrate {
public function hydrate($config, $data)
{
foreach($config as $attributeName => $column)
{
if(property_exists($this, $attributeName)) {
$this->{$attributeName} = $data[$column];
}
throw \Exception('The property %s does not exists in %s', $attributeName, self::class);
}
}
}
$legendaryWarrior = new Warrior;
$legendaryWarrior->hydrate($config, $data);
var_dump($legendaryWarrior);
$config = ['classicalName' => 'classical_name'];
$data = ['classical_name' => 'John'];
$testWarrior = new Warrior;
$testWarrior->hydrate($config, $data);
var_dump($testWarrior);