//定义抽象类'HTMLElement' abstract class HTMLElement{ protected $attributes; protected function __construct($attributes){ if(!is_array($attributes)){ throw new Exception('Invalid attribute type'); } $this->attributes=$attributes; } // 抽象的'getHTML()'方法 abstract protected function getHTML(); } //定义具体的类'Div'-扩展HTMLElement class Div extends HTMLElement{ private $output='<div '; private $data; public function __construct($attributes=array(),$data){ parent::__construct($attributes); $this->data=$data; } //'getHTML()'方法的具体实现 public function getHTML(){ foreach($this->attributes as $attribute=>$value){ $this->output.=$attribute.'="'.$value.'" '; } $this->output=substr_replace($this->output,'>',-1); $this->output.=$this->data.'</div>'; return $this->output; } } //定义具体类'Header1'-扩展HTMLElement class Header1 extends HTMLElement{ private $output='<h1 '; private $data; public function __construct($attributes=array(),$data){ parent::__construct($attributes); $this->data=$data; } //'getHTML()'方法的具体的实现 public function getHTML(){ foreach($this->attributes as $attribute=>$value){ $this->output.=$attribute.'="'.$value.'" '; } $this->output=substr_replace($this->output,'>',-1); $this->output.=$this->data.'</h1>'; return $this->output; } } //定义具体类'Paragraph'-扩展HTMLElement class Paragraph extends HTMLElement{ private $output='<p '; private $data; public function __construct($attributes=array(),$data){ parent::__construct($attributes); $this->data=$data; } //'getHTML()'方法的具体实现 public function getHTML(){ foreach($this->attributes as $attribute=>$value){ $this->output.=$attribute.'="'.$value.'" '; } $this->output=substr_replace($this->output,'>',-1); $this->output.=$this->data.'</p>'; return $this->output; } } //定义具体类'UnorderedList'-扩展HTMLElement class UnorderedList extends HTMLElement{ private $output='<ul '; private $items=array(); public function __construct($attributes=array(),$items=array()){ parent::__construct($attributes); if(!is_array($items)){ throw new Exception('Invalid parameter for list items'); } $this->items=$items; } //'getHTML()'方法的具体实现 public function getHTML(){ foreach($this->attributes as $attribute=>$value){ $this->output.=$attribute.'="'.$value.'" '; } $this->output=substr_replace($this->output,'>',-1); foreach($this->items as $item){ $this->output.='<li>'.$item.'</li>'; } $this->output.='</ul>'; return $this->output; } } |
class PageGenerator{ private $output='; private $title; public function __construct($title='Default Page'){ $this->title=$title; } public function doHeader(){ $this->output='<html><head><title>'.$this- >title.'</title></head><body>'; } public function addHTMLElement($htmlElement){ $this->output.=$htmlElement->getHTML(); } public function doFooter(){ $this->output.='</body></html>'; } public function fetchHTML(){ return $this->output; } } |
try{ //生成一些HTML元素 $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')); //实例化页面生成器类 $pageGen=new Page生成器(); $pageGen->doHeader(); // 添加'HTMLElement'对象 $pageGen->addHTMLElement($h1); $pageGen->addHTMLElement($div); $pageGen->addHTMLElement($par); $pageGen->addHTMLElement($ul); $pageGen->doFooter(); //显示网面 echo $pageGen->fetchHTML(); } catch(Exception $e){ echo $e->getMessage(); exit(); } |
try{ //生成一些HTML元素 $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')); //实例化页面生成器类 $pageGen=new Page生成器(); $pageGen->doHeader(); //添加'HTMLElement'对象 $pageGen->addHTMLElement($fakeobj) //把并不存在的对象传递 到这个方法 $pageGen->addHTMLElement($div); $pageGen->addHTMLElement($par); $pageGen->addHTMLElement($ul); $pageGen->doFooter(); // 显示网面 echo $pageGen->fetchHTML(); } catch(Exception $e){ echo $e->getMessage(); exit(); } |
$pageGen->addHTMLElement($fakeobj)//把不存在的对象传递到这个方法 |
Fatal error: Call to a member function on a non-object in path/to/file |
if (object instanceof class name){ //做一些有用的事情 } |
class PageGenerator{ private $output='; private $title; public function __construct($title='Default Page'){ $this->title=$title; } public function doHeader(){ $this->output='<html><head><title>'.$this->title.'</title></head><body>'; } public function addHTMLElement($htmlElement){ if(!$htmlElement instanceof HTMLElement){ throw new Exception('Invalid (X)HTML element'); } $this->output.=$htmlElement->getHTML(); } public function doFooter(){ $this->output.='</body></html>'; } public function fetchHTML(){ return $this->output; } } |
try{ //生成一些HTML元素 $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'); $teststr='This is not a HTML element'; //实例化页面生成器类 $pageGen=new Page生成器(); $pageGen->doHeader(); //添加'HTMLElement'对象 $pageGen->addHTMLElement($teststr) //把简单的字符串传递到这个方法 $pageGen->addHTMLElement($h1); $pageGen->addHTMLElement($div); $pageGen->addHTMLElement($par); $pageGen->doFooter(); //显示网页 echo $pageGen->fetchHTML(); } catch(Exception $e){ echo $e->getMessage(); exit(); } |
Invalid (X)HTML element |
class Div extends HTMLElement{ private $output='<div '; private $data; public function __construct($attributes=array(),$data){ if(!$data instanceof HTMLElement&&!is_string($data)){ throw new Exception('Invalid parameter type'); } parent::__construct($attributes); $this->data=$data; } //'getHTML()'方法的具体实现 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.='</div>'; return $this->output; } } class Header1 extends HTMLElement{ private $output='<h1 '; private $data; public function __construct($attributes=array(),$data){ if(!$data instanceof HTMLElement&&!is_string($data)){ throw new Exception('Invalid parameter type'); } parent::__construct($attributes); $this->data=$data; } //'getHTML()'方法的具体实现 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.='</h1>'; return $this->output; } } class Paragraph extends HTMLElement{ private $output='<p '; private $data; public function __construct($attributes=array(),$data){ if(!$data instanceof HTMLElement&&!is_string($data)){ throw new Exception('Invalid parameter type'); } parent::__construct($attributes); $this->data=$data; } //'getHTML()'方法的具体实现 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.='</p>'; return $this->output; } } class UnorderedList extends HTMLElement{ private $output='<ul '; private $items=array(); public function __construct($attributes=array(),$items=array()){ parent::__construct($attributes); if(!is_array($items)){ throw new Exception('Invalid parameter for list items'); } $this->items=$items; } //'getHTML()'方法的具体实现 public function getHTML(){ foreach($this->attributes as $attribute=>$value){ $this->output.=$attribute.'="'.$value.'" '; } $this->output=substr_replace($this->output,'>',-1); foreach($this->items as $item){ $this->output.=($item instanceof HTMLElement)?'<li>'.$item->getHTML().'</li>':'<li>'.$item.'</li>'; } $this->output.='</ul>'; return $this->output; } } |
if(!$data instanceof HTMLElement&&!is_string($data)){ throw new Exception('Invalid parameter type'); } |
$this->output.=($this->data instanceof HTMLElement)?$this->data- >getHTML():$this->data; |