php - Independent getter/setter methods, or combined? -



php - Independent getter/setter methods, or combined? -

while working on project, i've been making changes , browsing around existing framework api docs insight.

while perusing kohana docs, noticed getters/setters of given class typically combined:

public function someproperty($value = null){ if(is_null($value){ homecoming $this->_someproperty; } $this->_someproperty = $value; homecoming $this; }

rather than:

public function setsomeproperty($value){ $this->_someproperty = $value; homecoming $this; } public function getsomeproperty(){ homecoming $this->_someproperty; }

is there value in doing (the former), beyond lessening method count of given class? under understanding methods (functions in general) should more descriptive of action. other experienced developers cringe, tiny bit, when see this?

i surprised see popular framework utilize such conventions (i haven't used kohana of course)

i consider bad practise because violates commandqueryseparation. setting value changing state (command). getting value asking state (query). method should not both, 1 thing only.

also, it's not obvious method when it's called username, e.g. not have verb, or set. gets worse in example, because homecoming value either object or property value, not consistent.

moreover, getters (and setters) should used sparingly convolute api. more getters , setters have, more knowledge object required collaborators of object. if find objects asking other objects internals, chances misplaced responsibilities.

php oop naming-conventions kohana getter-setter

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -