私有变量
class Input {
private
$form;
public function __construct (Form $form) {
$this->form = $form;
}
public function getForm () {
return $this->form;
}
}
带有静态变量的方法
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;
}
}
我更喜欢后者,因为转储对象$form
时不包括属性。Input
由于它们的循环关系(示例中未显示),它使输出不可读。
使用后一种方法 VS 后一种方法有什么缺点?