When creating classes, I followed OO conventions and declared all class variables before using them:
class myClass {
private $property1, $property2, ...;
public __constructor() {
$this->property1 = $this->property2 = NULL;
}
}
But I realized that PHP is a scripting language, and not strictly adhere to the concepts of OO, so we can dynamically generate a class property:
class myClass {
public __constructor() {
$this->fields = $this->db->getFields(TABLE_NAME);
foreach($this->fields as $fld) {
$this->{$fld} = NULL;
}
}
}
Is this a good approach? I think that dynamically generated properties will have public access by default, so this may be one of the drawbacks, and such automation may be one of the advantages. Is there a difference in performance?
source
share