attributes=$attributes; } // abstract 'getHTML()' method abstract protected function getHTML(); } // define 'Div' class class Div extends HTMLElement{ private $output='
data=$data; } // concrete implementation for 'getHTML()' method public function getHTML(){ foreach($this->attributes as $attribute=>$value){ $this->output.=$attribute.'="'.$value.'" '; } $this->output=substr_replace($this->output,'>',-1); $this->output.=($this->data instanceof HTMLElement)? $this->data->getHTML():$this->data; $this->output.='
'; return $this->output; } } // define 'Header1' class class Header1 extends HTMLElement{ private $output='

data=$data; } // concrete implementation for 'getHTML()' method public function getHTML(){ foreach($this->attributes as $attribute=>$value){ $this->output.=$attribute.'="'.$value.'" '; } $this->output=substr_replace($this->output,'>',-1); $this->output.=($this->data instanceof HTMLElement)? $this->data->getHTML():$this->data; $this->output.='

'; return $this->output; } } // define 'Paragraph' class class Paragraph extends HTMLElement{ private $output='

data=$data; } // concrete implementation for 'getHTML()' method public function getHTML(){ foreach($this->attributes as $attribute=>$value){ $this->output.=$attribute.'="'.$value.'" '; } $this->output=substr_replace($this->output,'>',-1); $this->output.=($this->data instanceof HTMLElement)? $this->data->getHTML():$this->data; $this->output.='

'; return $this->output; } } // define 'UnorderedList' class class UnorderedList extends HTMLElement{ private $output=''; return $this->output; } } class PageGenerator{ private $output=''; private $title; public function __construct($title='Default Page'){ $this->title=$title; } public function doHeader(){ $this->output=''.$this->title.''; } public function addHTMLElements($htmlElements){ foreach($htmlElements as $htmlElement){ if(is_array($htmlElement)){ $this->addHTMLElements($htmlElement); } else{ $this->output.=$htmlElement->getHTML(); } } } public function doFooter(){ $this->output.=''; } public function fetchHTML(){ return $this->output; } } // spawn some HTML elements $h1=new Header1(array ('name'=>'header1','class'=>'headerclass'),'Content for H1 element goes here'); $div=new Div(array('name'=>'div1','class'=>'divclass'),'Content for Div element goes here'); $par=new Paragraph(array ('name'=>'par1','class'=>'parclass'),'Content for Paragraph element goes here'); $ul=new UnorderedList(array ('name'=>'list1','class'=>'listclass'),array ('item1'=>'value1','item2'=>'value2','item3'=>'value3')); // make recursive array with HTML objects $htmlElements=array($h1,array($div,$par,$ul)); // instantiate 'PageGenerator' object $pageGen=new PageGenerator(); // generate web page $pageGen->doHeader(); $pageGen->addHTMLElements($htmlElements); $pageGen->doFooter(); // display web page echo $pageGen->fetchHTML(); ?>