直接在__construct
中设置属性是不好的做法。这些代码块之一是否更适合面向对象的PHP?直接在__construct中设置属性是不好的做法吗?
我知道这是不好的做法,这样做的类外...
class person {
protected $name;
function __construct($persons_name) {
$this->set_name($persons_name);
}
function set_name($new_name){
$this->name = $new_name;
}
}
VS.
class person {
protected $name;
function __construct($persons_name) {
$this->name = $persons_name;
}
function set_name($new_name){
$this->name = $new_name;
}
}
无论如何,我认为''set_name()'是不好的练习,因为你应该使用'__set()'。其他人则认为,获得者和制定者都是不好的练习。拿你的选择。 – 2013-12-13 19:49:03
有没有什么好书可以学习正确的PHP OOP方法? –
'set_name()'在这里是多余的,因为你的属性是公共的。为什么有人要用一种方法来改变它呢?让它保护或私人只是这样你可以有一个理由使用该方法也不是一个好主意。安装程序适用于有充分理由直接无法访问的属性 –