<?php
//Programowanie obiektowe w języku PHP5 / Moduł 4. Algorytmy i struktury danych / Cz. 3
// wzorzec budowniczy
class HTMLBuilder {
private $html;
private function __construct($html) {
$this->html = $html;
}
public static function begin() {
$builder = new HTMLBuilder('<html><head></head><body>');
return $builder;
}
public function p($paragraph) {
$this->html .= '<p>' . $paragraph . '</p>';
return $this;
}
public function hr() {
$this->html .= '<hr>';
return $this;
}
public function pre($contents) {
$safecontents = htmlspecialchars($contents);
$this->html .= "<pre>$safecontents</pre>";
return $this;
}
public function end() {
$this->html .= '</body></html>';
return $this->html;
}
}
$test = HTMLBuilder::begin()
->p('paragraf')
->hr()
->pre('class HTMLBuilder')
->p('kolejny paragraf')
->end();
echo '<pre>';
var_dump($test);
echo '</pre>';