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

2016-01-29 13:00 38 1 收藏

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

【 tulaoshi.com - PHP 】

四、 扩展"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');

来源:https://www.tulaoshi.com/n/20160129/1488465.html

延伸阅读
布尔操作符(Boolean operator)是求值结果要么为true,要么为false的一种操作符。C#提供了几个非常有用的布尔操作符,其中最简单的是NOT(求反)操作符,它使用感叹号(!)来表示。!操作符求一个布尔值的反值。在上例中,假如变量areYouReady的值为true,那么表达式!areYouReady的求值结果就是false。 理解相等和关系操作符 两个更常...
用转换操作符保护代码的安全 作者:Danny Kalev 编译:MTT 工作室 原文出处:Preserve Code Safety with Conversion Operators 摘要: 不经意的对象转换常常严重地危害代码的安全。幸运的是,转换操作符允许你根据实际情况来启用和禁用转换,这有助于避免出现病态行...
赋值语句 前面已经说明,要访问内存,就需要相应的地址以表明访问哪块内存,而变量是一个映射,因此变量名就相当于一个地址。对于内存的操作,在一般情况下就只有读取内存中的数值和将数值写入内存(不考虑分配和释放内存),在C++中,为了将一数值写入某变量对应的地址所标识的内存中(出于简便,以后称变量a对应的地址为变量a的地址...
checked和unchecked操作符用于整型算术运算时控制当前环境中的溢出检查。下列运算参与了checked和unchecked检查(操作数均为整数):1)  预定义的++和――一元运算符。2)  预定义的-一元运算符。3)  预定义的+、-、×、/等二元操作符。4)  从一种整型到另一种整型的显示数据转换。 当上述整型运算产生一个目标类型...
unit WinForm;interfaceuses System.Drawing, System.Collections, System.ComponentModel, System.Windows.Forms, System.Data;type TWinForm = class(System.Windows.Forms.Form) {$REGION 'Designer Managed Code'} strict private /// summary /// Required designer variable. /// /summary Components: System....

经验教程

537

收藏

40
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部