当前位置:中国站长下载文章中心网页编程PHP编程 → PHP强制对象类型之instanceof操作符(5)

PHP强制对象类型之instanceof操作符(5)

减小字体 增大字体 作者:不详  来源:不详  发布时间:2006-8-14 10:18:39
     四、 扩展"instanceof"操作符的使用:嵌套(X)HTML widget
  
    好。你已经看到了"instanceof"操作符在被直接注入到页面生成器类的输入对象进行类型检查方面所表现出的良好功能。现在,我将再进一步来把一个检查例程添加到(X)HTML widget类的构造器和"getHTML()"方法中,这样它们可以接受其它的widget作为输入参数。请检查下面改进的类:
  
  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;
  }
  }
  
    如上面的类所展示的,为了允许在生成相应的网页时实现嵌套的(X)HTML元素,我分别重构了它们的构造器和"getHTML()"方法。请注意,我在每一个类的构造器中包含了下面的条件块:
  
  if(!$data instanceof HTMLElement&&!is_string($data)){
  throw new Exception('Invalid parameter type');
  }
  
    至此,我实际做的是确保仅有字符串数据和"HTMLElement"类型对象允许作为每一个类的输入参数。否则,将分别由各自方法抛出一个异常,并且有可能导致应用程序的停止执行。所以,这就是对输入数据的检查过程。现在,让我们看一下"getHTML()"方法的新的签名,其中也使用了"instanceof"操作符:
  
  $this->output.=($this->data instanceof HTMLElement)?$this->data-
  >getHTML():$this->data;
  
    如你所见,在这种情况下,对于利用(X)HTML widget类的多态性特征方面this操作符是非常有用的。如果$data属性也是一个widget,那么它的"getHTML()"方法将被正确调用,这会导致显示嵌套的网页元素。另一方面,如果它仅是一个字符串,那么它就被直接添加到当前类的所有输出上。
  
    至此,为了确保某些对象属于一个特定的类型,你可能已经理解了PHP 5中"instanceof"操作符的用法。正如你在本文中所见,在PHP 5中强制对象类型其实是一个相当直接的工作。现在,你最好开发一个使用这个方法来过滤你的PHP应用程序中的对象的例子来加深自己的理解。
  
    五、小结
  
    在本文中,你学习了如何使用PHP 5中的"instanceof"操作符来检查你的输入对象的类型;然而,我所向你展示的方法不是唯一的。在后面的一篇中,我将向你解释怎样实现PHP 5中的良好的"类型提示"特征,这是实现强制对象类型的另外一种方法。
  
  作者:朱先忠编译出处:天极开发
    做人要厚道,请注明转自chinazhan中国站长(www.ChinaZhan.com)。