メソッドチェーン
メソッドチェーン( こんなやつ → $xxx->xxx()->xxx() )が使ってみたかったので試しに作成してみた。
Class hoge { public function this() { return $this; } public function hello() { echo 'hello'; } } $hoge = new hoge(); $hoge->this()->hello(); // 実行結果:hello
要はオブジェクトを返すメソッドを定義してやれば良いみたい
オマケ:
つなげて「Hello.World!」を作成してみた。
Class hoge { public function hello() { echo 'Hello.'; return $this; } public function world() { echo 'World!'; return $this; } } $hoge = new hoge(); $hoge->hello()->world(); // 実行結果:Hello.World!