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:
- Is there a performance-reason to pick one way over the other? 2)
- Is there a OOP-logic/ease-of-programming/other-reason to pick one way over the other?