
日期:2006-09-28 作者:喜騰小二 來源:PHPChina
一、簡介
在PHP中實現強制物件類型有時可能非常重要。如果缺少了它,或是因為缺乏這方麵的知識——基於不正確的編程假設,或者僅僅是由於懶惰,那麼妳會在特定的Web應用程式中看到妳所不希望的結果。特別是當用PHP
4進行編程時,使用"is_a()"函式(儘管還有其它方法)來驗證妳所使用的物件的類型是非常容易的事情。毫無疑問,強制物件類型還可以被用於篩選輸入物件(需要被作為參數傳遞到同一個應用程式中的其它PHP類)。
不過,PHP
4並沒有暴露一些有關於它的物件模型的弱點-為了實現某些在成熟的麵嚮物件的語言中出現的特徵,它偶而可能要求編寫另外的程式碼。長時間以來,這一事實已經為PHP社區眾所周知。然而,隨着PHP
5的發行,許多這些極有價值的特徵作為改進的物件模型的一部分被新增到其中。它們將有助於更為緊密地實現基於物件的程式碼的開發-允許妳使用特定的物件特徵。
在上麵的情況下,當涉及到物件類型強制時應該特別注意。實際上,在一個Web應用程式的執行期間,PHP
5提供給開發者至少兩種方法來檢查物件類型——它們分別是“instanceof”運運算元和“類型提示”特徵。現在轉到本文的主題,我將介紹PHP
5中"instanceof"運運算元的使用;妳很快就會發現,它可以非常方便地用來確定是否妳正在使用的物件屬於一個特定的類型。
本文將透過一些麵嚮物件的範例來說明妳理解如何在PHP 5中實現強制物件類型。
二、
妳不該做什麼
為了展示在PHP 5中如何實現物件類型強制,我將使用(X)HTML
widget類,還有一個簡單的页面生成器類,並作了簡單的修改以適合PHP
5開發環境。
我的第一個範例列舉了一些派生自一個抽象的基類"HTMLElement"的(X)HTML
widget類,它跳過了到它們的輸入物件類型的檢查。請先看下麵的類:
| //定義抽象類'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 |