在创建类,我也跟着OO公约和使用它们之前声明的所有类变量:在PHP中创建动态类变量是个好主意吗?
class myClass {
private $property1, $property2, ...;
public __constructor() {
$this->property1 = $this->property2 = NULL;
}
}
但我意识到PHP是脚本语言,而不是下面的面向对象的概念严格,所以我们可以动态'产生阶级属性:
class myClass {
public __constructor() {
$this->fields = $this->db->getFields(TABLE_NAME);
foreach($this->fields as $fld) {
$this->{$fld} = NULL;
}
}
}
这是一个好方法吗? 我认为动态生成的属性默认情况下具有公共访问权限,所以这可能是一个缺点,并且这种自动化可能是一个优点。 性能方面有什么不同吗?
可能重复[为什么在PHP中隐含的财产申报?](http://stackoverflow.com/questions/6027861/why-implicit-property -declaration-in-php) – 2011-05-21 07:47:12