Prototypeパターン
オブジェクトからオブジェクトを生成する設計。
phpではcloneキーワードでオブジェクトのシャローコピーを生成できる。
<? class Widget{ public $header; function __construct(){ $this->header = new Header($title); //<-- headerプロパティはHeaderオブジェクトの参照である } /* * phpのクローンキーワードは、オブジェクトのシャローコピーを行う * ディープコピーを行うときは以下のように__cloneメッソドを実装して、 * それぞれのプロパティのクローンを作成する必要がある */ function __clone(){ $this->header = clone $this->header; } function title(){ return $this->header->title; } } class Header{ public $title = "default title"; } $widget_a = new Widget; echo $widget_a->title() . "<br />"; //#default title $widget_b = clone $widget_a; echo $widget_a->title() . "<br />"; //#default title $widget_a->header->title = "hogehoge"; echo $widget_a->title() . "<br />"; //#hogehoge echo $widget_b->title() . "<br />";//#default titleになる(__cloneが実装されていないとhogehogeのまま) ?>