- ·上一篇文章:PHPUnit袖珍指南之安装PHPUnit
- ·下一篇文章:在PHP中开发XML应用程序之基础篇
PHP强制对象类型之instanceof操作符
'Invalidparametertype');
}
parent::__construct($attributes);
$this->data=$data;
}
//'getHTML()'方法的具体实现
publicfunctiongetHTML(){
foreach($this->attributesas$attribute=>$value){
$this->output.=$attribute.'="'.$value.'"';
}
$this->output=substr_replace($this->output,'>',-1);
$this->output.=($this->datainstanceofHTMLElement)?
$this->data->getHTML():$this->data;
$this->output.='</div>';
return$this->output;
}
}
classHeader1extendsHTMLElement{
private$output='<h1';
private$data;
publicfunction__construct($attributes=array(),$data){
if(!$datainstanceofHTMLElement&&!is_string($data)){
thrownewException('Invalidparametertype');
}
parent::__construct($attributes);
$this->data=$data;
}
//'getHTML()'方法的具体实现
publicfunctiongetHTML(){
foreach($this->attributesas$attribute=>$value){
$this->output.=$attribute.'="'.$value.'"';
}
$this->output=substr_replace($this->output,'>',-1);
$this->output.=($this->datainstanceofHTMLElement)?
$this->data->getHTML():$this->data;
$this->output.='</h1>';
return$this->output;
}
}
classParagraphextendsHTMLElement{
private$output='<p';
private$data;
publicfunction__construct($attributes=array(),$data){
if(!$datainstanceofHTMLElement&&!is_string($data)){
thrownewException('Invalidparametertype');
}
parent::__construct($attributes);
$this->data=$data;
}
//'getHTML()'方法的具体实现
publicfunctiongetHTML(){
foreach($this->attributesas$attribute=>$value){
$this->output.=$attribute.'="'.$value.'"';
}
$this->output=substr_replace($this->output,'>',-1);
$this->output.=($this->datainstanceofHTMLElement)?
$this->data->getHTML():$this->data;
$this->output.='</p>';
return$this->output;
}
}
classUnorderedListextendsHTMLElement{
private$output='<ul';
private$items=array();
publicfunction__construct($attributes=array(),$items=array()){
parent::__construct($attributes);
if(!is_array($items)){
thrownewException('Invalidparameterforlistitems');
}
$this->items=$items;
}
//'getHTML()'方法的具体实现
publicfunctiongetHTML(){
foreach($this->attributesas$attribute=>$value){
$this->output.=$attribute.'="'.$value.'"';
}
$this->output=substr_replace($this->output,'>',-1);
foreach($this->itemsas$item){
$this->output.=($iteminstanceof
HTMLElement)?'<li>'.$item->getHTML().'</li>':'<li>'.$item.'</li>';
}
$this->output.='</ul>';
return$this->output;
}
}
如上面的类所展示的,为了允许在生成相应的网页时实现嵌套的(X)HTML元素,我分别重构了它们的构造器和"getHTML()"方法。请注意,我在每一个类的构造器中包含了下面的条件块:
if(!$datainstanceofHTMLElement&&!is_string($data)){
thrownewException('Invalidparametertype');
}
至此,我实际做的是确保仅有字符串数据和"HTMLElement"类型对象允许作为每一个类的输入参数。否则,将分别由各自方法抛出一个异常,并且有可能导致应用程序的停止执行。所以,这就是对输入数据的检查过程。现在,让我们看一下"getHTML()"方法的新的签名,其中也使用了"instanceof"操作符:
$this->output.=($this->datainstanceofHTMLElement)?$this->data-
>getHTML():$this->data;
如你所见,在这种情况下,对于利用(X)HTMLwidget类的多态性特征方面this操作符是非常有用的。如果$data属性也是一个widget,那么它的"getHTML()"方法将被正确调用,这会导致显示嵌套的网页元素。另一方面,如果它仅是一个字符串,那么它就被直接添加到当前类的所有输出上。
至此,为了确保某些对象属于一个特定的类型,你可能已经理解了PHP5中"instanceof"操作符的用法。正如你在本文中所见,在PHP5中强制对象类型其实是一个相当直接的工作。现在,你最好开发一个使用这个方法来过滤你的PHP应用程序中的对象的例子来加深自己的理解。
五、小结
在本文中,你学习了如何使用PHP5中的"instanceof"操作符来检查你的输入对象的类型;然而,我所向你展示的方法不是唯一的。在后面的一篇中,我将向你解释怎样实现PHP5中的良好的"类型提示"特征,这是实现强制对象类型的另外一种方法。
}
parent::__construct($attributes);
$this->data=$data;
}
//'getHTML()'方法的具体实现
publicfunctiongetHTML(){
foreach($this->attributesas$attribute=>$value){
$this->output.=$attribute.'="'.$value.'"';
}
$this->output=substr_replace($this->output,'>',-1);
$this->output.=($this->datainstanceofHTMLElement)?
$this->data->getHTML():$this->data;
$this->output.='</div>';
return$this->output;
}
}
classHeader1extendsHTMLElement{
private$output='<h1';
private$data;
publicfunction__construct($attributes=array(),$data){
if(!$datainstanceofHTMLElement&&!is_string($data)){
thrownewException('Invalidparametertype');
}
parent::__construct($attributes);
$this->data=$data;
}
//'getHTML()'方法的具体实现
publicfunctiongetHTML(){
foreach($this->attributesas$attribute=>$value){
$this->output.=$attribute.'="'.$value.'"';
}
$this->output=substr_replace($this->output,'>',-1);
$this->output.=($this->datainstanceofHTMLElement)?
$this->data->getHTML():$this->data;
$this->output.='</h1>';
return$this->output;
}
}
classParagraphextendsHTMLElement{
private$output='<p';
private$data;
publicfunction__construct($attributes=array(),$data){
if(!$datainstanceofHTMLElement&&!is_string($data)){
thrownewException('Invalidparametertype');
}
parent::__construct($attributes);
$this->data=$data;
}
//'getHTML()'方法的具体实现
publicfunctiongetHTML(){
foreach($this->attributesas$attribute=>$value){
$this->output.=$attribute.'="'.$value.'"';
}
$this->output=substr_replace($this->output,'>',-1);
$this->output.=($this->datainstanceofHTMLElement)?
$this->data->getHTML():$this->data;
$this->output.='</p>';
return$this->output;
}
}
classUnorderedListextendsHTMLElement{
private$output='<ul';
private$items=array();
publicfunction__construct($attributes=array(),$items=array()){
parent::__construct($attributes);
if(!is_array($items)){
thrownewException('Invalidparameterforlistitems');
}
$this->items=$items;
}
//'getHTML()'方法的具体实现
publicfunctiongetHTML(){
foreach($this->attributesas$attribute=>$value){
$this->output.=$attribute.'="'.$value.'"';
}
$this->output=substr_replace($this->output,'>',-1);
foreach($this->itemsas$item){
$this->output.=($iteminstanceof
HTMLElement)?'<li>'.$item->getHTML().'</li>':'<li>'.$item.'</li>';
}
$this->output.='</ul>';
return$this->output;
}
}
如上面的类所展示的,为了允许在生成相应的网页时实现嵌套的(X)HTML元素,我分别重构了它们的构造器和"getHTML()"方法。请注意,我在每一个类的构造器中包含了下面的条件块:
thrownewException('Invalidparametertype');
}
至此,我实际做的是确保仅有字符串数据和"HTMLElement"类型对象允许作为每一个类的输入参数。否则,将分别由各自方法抛出一个异常,并且有可能导致应用程序的停止执行。所以,这就是对输入数据的检查过程。现在,让我们看一下"getHTML()"方法的新的签名,其中也使用了"instanceof"操作符:
>getHTML():$this->data;
如你所见,在这种情况下,对于利用(X)HTMLwidget类的多态性特征方面this操作符是非常有用的。如果$data属性也是一个widget,那么它的"getHTML()"方法将被正确调用,这会导致显示嵌套的网页元素。另一方面,如果它仅是一个字符串,那么它就被直接添加到当前类的所有输出上。
至此,为了确保某些对象属于一个特定的类型,你可能已经理解了PHP5中"instanceof"操作符的用法。正如你在本文中所见,在PHP5中强制对象类型其实是一个相当直接的工作。现在,你最好开发一个使用这个方法来过滤你的PHP应用程序中的对象的例子来加深自己的理解。
五、小结
在本文中,你学习了如何使用PHP5中的"instanceof"操作符来检查你的输入对象的类型;然而,我所向你展示的方法不是唯一的。在后面的一篇中,我将向你解释怎样实现PHP5中的良好的"类型提示"特征,这是实现强制对象类型的另外一种方法。
