1
私有变量与静态变量私有财产和持有静态价值的方法之间的区别?
class Input {
public function __construct (Form $form) {
$this->getForm($form);
}
public function getForm (Form $set_form = null) {
static $form;
if (!$form && $set_form !== null) {
$form = $set_form;
} else if ($form && $set_form) {
throw new \ErrorException('Form has been already set.');
}
return $form;
}
}
class Input {
private
$form;
public function __construct (Form $form) {
$this->form = $form;
}
public function getForm() {
return $this->form;
}
}
方法我更喜欢后者,因为当你倾销Input
对象不包括$form
财产。由于它们的循环关系(在示例中未显示),它使输出不可读。
使用后一种方法VS后者有什么缺点?