"Avoid writing naive setters and getters" Hum... oui mais bon...
>>Encapsulation is important in any OO language, popularity has nothing to do with it. In dynamically typed languages, like PHP, it is especially useful because there is little ways to ensure a property is of a specific type without using setters.
en plus :
Why use getters and setters?
* Scalability: It's easier refactor a getter than search all the var assignments in a project code.
* Debugging: You can put breakpoints at setters and getters.
* Cleaner: Magic functions are not good solution for writting less, your IDE will not suggest the code. Better use templates for fast-writting getters.
et sinon les magic_methods ?? (
http://stackoverflow.com/questions/4478661/getter-and-setter)
<?php
class MyClass {
private $firstField;
private $secondField;
public function __get($property) {
if (property_exists($this, $property)) {
return $this->$property;
}
}
public function __set($property, $value) {
if (property_exists($this, $property)) {
$this->$property = $value;
}
return $this;
}
}
?>
Ben là, y'a pas d'intérêt sauf économique... (flemme !) autant passer par des attributs public si on ne fait rien d'autre qu'un raccourci...
Sinon, ben des templates...