I have an object that contains around 30 settings. 1 page can contain 10 objects that have max 15 settings each. To initialize the object, I get an associative array from the database for each object. I am wondering what would be the best performance? I currently have (example):
$object = new element_class();
$array = <query from database>
//Reinitialize object to remove any set values of object properties to null
foreach($object as $key=>$value)
{
$object->$key = null;
}
//Switch of array to set the values in the array in the object properties
foreach($array as $key=>$value)
{
switch($key)
{
case 'a':
$this->property_a = $value;
break;
case 'b':
$this->property_b = $value;
break;
case 'c':
$this->property_c = $value;
break;
case 'd':
$this->property_d = $value;
break;
etc....
}
}
//Default section to set necessary properties if not in database array
foreach($object as $key=>$value)
{
if($value == null)
{
switch ($key)
{
case 'property_a':
$object->$key = <any value here>;
break;
case 'property_b':
$object->$key = <any value here>;
break;
case 'property_c':
$object->$key = <any value here>;
break;
etc.....
}
}
}
Is there a faster way for these 3 steps? Any performance increase is welcome, especially because there are so many of it...
Sorry if there are errors in the script, I typed this from head, I dont have the code atm