3

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

0

2 Answers 2

2

This

$object = new element_class();

//Reinitialize object to remove any set values of object properties to null
foreach($object as $key=>$value)
    $object->$key = null;

can be replaced by just

$object = new element_class();

assuming you define public $fields, which are initialized to null by default.

This

$array = <query from database>
//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....
    }
}

can be replaced by

$settings = (object) $array;

It keeps the settings separate from $this, so you can easily save them.

This

//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.....
            }   

should have been done by the element_class() constructor as follows:

class element_class {
    public $a = <any value here>;
    public $b = <any value here>;
    ....
}

You then eliminate the null loop (or rather replace it with setting defaults instead), and all you have to do is override them with with the values fetched from <query>.


Here's a way using php's native array_merge:

$defaults = [
  'a' => <any value here>,
  'b' => <any value here>,
  ...
];
$array = <query>;
$settings = (object) array_merge( $defaults, $array );

I don't know if it's faster, but it is much more easily maintained. If there is any speed concern at all, it should be with the number of SQL queries. You say you have "10" objects each with around "15" rows. You'll get far more speed gain by making that 1 query returning around 150 objects.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your responce. I don't really get the "(object) $array" part. What does it do with the object?
Oh, that just converts the array to a stdClass. So, if you have $a = array( 'foo' => 'bar'), you would access foo by writing $a['foo']. With $b = (object) $a, you would access it by writing $b->bar, which is more convenient, especially within "" strings: "... {$a['foo']}..." vs "... $b->foo...". But it is not necessary.
2

Instead of a switch, you can use variable properties, assuming there's a consistent pattern between the property names and the keys of the associative array.

$this->{"property_" . $key} = $value;

Another option would be to just store all the settings in a single property that contains an associative array. Then you can just do:

$this->settings = $array;

3 Comments

Not sure if OP wants to add all array values to the object. And maybe that's why he has a switch statement.
Maybe even $this->settings = (object) $array;.
It address his array iteration to set $object properties. I assume OP intends to access the settings using $object->settings (a bit easier than {$object['settings']} for example).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.