1

プライベート変数

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ます。それらの循環関係 (例には示されていません) のため、出力が判読不能になります。

後者のアプローチと後者のアプローチを使用することの欠点は何ですか?

4

1 に答える 1