I see two different implementations when people process classes that extend other classes and provide functionality based on specific settings within 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 example method I donβt like the duplicate field list, because the fields are actually defined as in $person->getFields() and $person->getRules() , and it must compute the array every time it is requested using the method. On the other hand, I do not like that each object saves all the settings in a variable. It is like a waste of resources. So I'm just wondering which is better.
My main questions are:
- Is there a performance reason to choose one path after another? 2)
- Is there OOP logic / ease of programming / another reason to choose one path through another?
source share