Design Patter by PHP[7]: Builder

Date
2006-12-03 (Sun)
Category
Design Pattern

第七回目は Builder です。

Class Diagram: Builder Pattern

また今回は、Browser から実行できるようにしてみました。以下を試してみてください。

Builder Pattern

コードはいつも通り、Subversion のレポジトリにあります。

http://svn.nydd.org/dpxphp/Builder/

今回の気づき

PHP の DOM 関数の注意

動作確認に使用したのは、PHP Version 5.1.2 / libxml 2.6.16 のようです。

PHP の DOM 関数では、独立した (DOMDocument に append されていない) DOMElement には appendChild できない。

どういうことか。以下のコード参照。

<?php
$domdoc = new DOMDocument();
$html = new DOMElement('html'); $head = new DOMElement('head'); $title = new DOMElement('title', 'WTF');
$head->appendChild($title);
$domdoc->appendChild($html); $html->appendChild($head); ?>

上記コードは、動かない。実行すると 8行目で 'No Modification Allowed Error' というエラーが出る。理由はよくわかりませんが… しかし独立していなければ、DOMElement も appendChild できる、ということは、下のように実行順序を変えるだけでこのコードは動く。

<?php
$domdoc = new DOMDocument();
$html = new DOMElement('html'); $head = new DOMElement('head'); $title = new DOMElement('title', 'WTF');
$domdoc->appendChild($html); $html->appendChild($head); $head->appendChild($title); ?>

もうちょっと書き方を変えると、以下のようにも書ける。

<?php
$domdoc = new DOMDocument();
$html = new DOMElement('html'); $head = $html->appendChild( new DOMElement('head') ); $title = $head->appendChild( new DOMElement('title', 'WTF') ); ?>

最後のが一番シンプルな気もするけれど、場合によりけりでしょう。一番意味の通るものを使うべきだと思います。

この Design Pattern by PHP シリーズがいつも参考にしているのは以下の本です。

Comment:5

Cheap Rift Gold:2011-09-15 (Thu) 23:07

Rado RADO has been the The definition of component parts 28 rubies thickness of Basel irresistible It is reported that .

Ugg Classic tall online:2011-09-18 (Sun) 21:26

The article in your blog reminds me some old memory .That is good .It gives me happy .I think we will have a harmonious talk.Do you agree?

gxwatches:2011-09-20 (Tue) 22:11

throng You express your individualitystyled dyed or permed just likeNobody wants the pieces to get lost in carpeting.

watches replica:2011-09-21 (Wed) 21:29

hobby whatever their requirementsInternet has made this beautiful timepiece moreThe following watches are those make great gifts.

louis vuitton outlet:2011-12-14 (Wed) 01:25

New style louis vuitton outlet are all come with our online store,
100% authentic and 100% Satisfaction 2011 louis vuitton bags are accumulation cheapest price.
High quality guarantee with amazing low price from our louis vuitton outlet store online,
Come and just enjoy shopping here, what you see will the same as what you get,
buy cheap louis vuitton online now!

Comment Form

Remember Me?


Trackback:0

TrackBack URL for this entry
http://blogs.grf-design.com/mt/mt-tb.cgi/198
Listed below are links to weblogs that reference
Design Patter by PHP[7]: Builder from The Croton

Return to Page Top