4

I see two different implementations when people handle classes that extend other classes and provide functionality based on certain setting inside the class.

  • Variables are used to store settings.
  • Methods are used to return settings.

Using Variables:

    class Model {
        var $fields = array();
        function getFields() {
            return array_keys($this->fields);   
        }
        function getRules() {
            return $this->fields;   
        }
    }
    class Person extends Model {
        var $fields = array(
            'name' => array('maxLength'=>10),
            'email' => array('maxLength'=>50, 'validEmail'=>true),
        );
    }

Using Methods:

    class Model {
        function getFields() {}
    }
    class Person extends Model {
        function getFields() {
            return array('name','email');
        }
        function getRules() {
            return array(
                'name' => array('maxLength'=>10),
                'email' => array('maxLength'=>50, 'validEmail'=>true),
            );
        }   
    }

Both examples achieve the same results, I can do things like $person->getFields() and $person->getRules(), but in the method-example I don't like the "duplicate" field list, because the fields are actually defined both in $person->getFields() and $person->getRules() and it must compute the array every time it is asked for via the method. On the other hand, I don't like that every object stores all the settings in a variable. It seems like a resource waste. So I'm just wondering what's the better way.

My main questions are:

  1. Is there a performance-reason to pick one way over the other? 2)
  2. Is there a OOP-logic/ease-of-programming/other-reason to pick one way over the other?
preyz
  • 3,029
  • 5
  • 29
  • 36

2 Answers2

1

From a few benchmark tests - the times are pretty similar - the exeption though

return array('name','email');

is much faster than

return array_keys($this->fields);

Running 10,000 operations for each method produced these averages:

Variable:
    getFields    0.06s
    getRules     0.05s
Method:
    getFields    0.04s
    getRules     0.05s

To answer your second question - it depends on your use-case - if the data stored in these objects is static, or if it will come from another datasource / config file.

One follow up question, why not use object properties?

class Person extends Model {
    protected $name
    protected $email

    public function getName() {
        return $this->name;
    }

    public function getEmail() {
        return $this->email;
    }

}
cloakedninjas
  • 4,007
  • 2
  • 31
  • 45
  • Using properties is included with the "using variables" options, there $this->fields is used to store an array of field names as keys with an array of rules as value. – preyz Aug 01 '12 at 19:41
  • sorry, my mistake, I was meaning use individual properties `$person->name` `$person->email` – cloakedninjas Aug 02 '12 at 09:31
1

My opinion is pick what you are comfortable with, there is no much performance loss or performance gain from using either. You better save the performance saving effort for data handling.

For me I use object properties, it looks clear when you are looking at the class, for storing such default properties, and if you want to override them, then use this beautiful syntax:

array()+array()
Omar
  • 8,374
  • 8
  • 39
  • 50