当前位置:Gxlcms > PHP教程 > php类型运算符"instanceof"操作符的扩展使用

php类型运算符"instanceof"操作符的扩展使用

时间:2021-07-01 10:21:17 帮助过:22人阅读

"instanceof"操作符在被直接注入到页面生成器类的输入对象进行类型检查方面所表现出的良好功能。现在,再进一步来把一个检查例程添加到(X)HTML widget类的构造器和"getHTML()"方法中,这样它们可以接受其它的widget作为输入参数。请检查下面改进的类:

  1. class Div extends HTMLElement{
  2.  private $output='<div ';
  3.  private $data;
  4.  public function construct($attributes=array(),$data){
  5.   if(!$data instanceof HTMLElement&&!is_string($data)){
  6.    throw new Exception('Invalid parameter type');
  7.   }
  8.   parent::construct($attributes);
  9.   $this->data=$data;
  10.  }
  11.  //'getHTML()'方法的具体实现
  12.  public function getHTML(){
  13.   foreach($this->attributes as $attribute=>$value){
  14.    $this->output.=$attribute.'="'.$value.'" ';
  15.   }
  16.   $this->output=substr_replace($this->output,'>',-1);
  17.   $this->output.=($this->data instanceof HTMLElement)?
  18.   $this->data->getHTML():$this->data;
  19.   $this->output.='</div>';
  20.   return $this->output;
  21.  }
  22. }
  23. class Header1 extends HTMLElement{
  24.  private $output='<h1 ';
  25.  private $data;
  26.  public function construct($attributes=array(),$data){
  27.   if(!$data instanceof HTMLElement&&!is_string($data)){
  28.    throw new Exception('Invalid parameter type');
  29.   }
  30.   parent::construct($attributes);
  31.   $this->data=$data;
  32.  }
  33.  //'getHTML()'方法的具体实现
  34.  public function getHTML(){
  35.   foreach($this->attributes as $attribute=>$value){
  36.    $this->output.=$attribute.'="'.$value.'" ';
  37.   }
  38.   $this->output=substr_replace($this->output,'>',-1);
  39.   $this->output.=($this->data instanceof HTMLElement)?
  40.   $this->data->getHTML():$this->data;
  41.   $this->output.='</h1>';
  42.   return $this->output;
  43.  }
  44. }
  45. class Paragraph extends HTMLElement{
  46.  private $output='<p ';
  47.  private $data;
  48.  public function construct($attributes=array(),$data){
  49.   if(!$data instanceof HTMLElement&&!is_string($data)){
  50.    throw new Exception('Invalid parameter type');
  51.   }
  52.   parent::construct($attributes);
  53.   $this->data=$data;
  54.  }
  55.  //'getHTML()'方法的具体实现
  56.  public function getHTML(){
  57.   foreach($this->attributes as $attribute=>$value){
  58.    $this->output.=$attribute.'="'.$value.'" ';
  59.   }
  60.   $this->output=substr_replace($this->output,'>',-1);
  61.   $this->output.=($this->data instanceof HTMLElement)?
  62.   $this->data->getHTML():$this->data;
  63.   $this->output.='</p>';
  64.   return $this->output;
  65.  }
  66. }
  67. class UnorderedList extends HTMLElement{
  68.  private $output='<ul ';
  69.  private $items=array();
  70.  public function construct($attributes=array(),$items=array()){
  71.   parent::construct($attributes);
  72.   if(!is_array($items)){
  73.    throw new Exception('Invalid parameter for list items');
  74.  }
  75.  $this->items=$items;
  76. }
  77. //'getHTML()'方法的具体实现
  78. public function getHTML(){
  79.  foreach($this->attributes as $attribute=>$value){
  80.   $this->output.=$attribute.'="'.$value.'" ';
  81.  }
  82.  $this->output=substr_replace($this->output,'>',-1);
  83.  foreach($this->items as $item){
  84.   $this->output.=($item instanceof
  85.   HTMLElement)?'<li>'.$item->getHTML().'</li>':'<li>'.$item.'</li>';
  86.  }
  87.  $this->output.='</ul>';
  88.  return $this->output;
  89. }
  90. }

 如上面的类所展示的,为了允许在生成相应的网页时实现嵌套的(X)HTML元素,分别重构了它们的构造器和"getHTML()"方法。请注意,在每一个类的构造器中包含了下面的条件块:

  1. if(!$data instanceof HTMLElement&&!is_string($data)){
  2. throw new Exception('Invalid parameter type');
  3. }

  至此,实际做的是确保仅有字符串数据和"HTMLElement"类型对象允许作为每一个类的输入参数。否则,将分别由各自方法抛出一个异常,并且有可能导致应用程序的停止执行。所以,这就是对输入数据的检查过程。现在,让我们看一下"getHTML()"方法的新的签名,其中也使用了"instanceof"操作符:

  1. $this->output.=($this->data instanceof HTMLElement)?$this->data-
  2. >getHTML():$this->data;

 如你所见,在这种情况下,对于利用(X)HTML widget类的多态性特征方面this操作符是非常有用的。如果$data属性也是一个widget,那么它的"getHTML()"方法将被正确调用,这会导致显示嵌套的网页元素。另一方面,如果它仅是一个字符串,那么它就被直接添加到当前类的所有输出上。

  至此,为了确保某些对象属于一个特定的类型,你可能已经理解了php 5中"instanceof"操作符的用法。正如你在本文中所见,在PHP 5中强制对象类型其实是一个相当直接的工作。现在,你最好开发一个使用这个方法来过滤你的PHP应用程序中的对象的例子来加深自己的理解。

  1. <?php
  2. //定义一个抽像类HTMLElement
  3. abstract class HTMLElement
  4. {
  5. protected $attributes;
  6. protected function construct($_attributes)
  7. {
  8. if(!is_array($_attributes))
  9. {
  10. throw new Exception("attributes not is array");
  11. }
  12. $this->attributes = $_attributes;
  13. }
  14. //定义一个虚函数
  15. abstract function getHTML();
  16. }
  17. //定义具体的类"Div"扩展HTMLElement
  18. class Div extends HTMLElement
  19. {
  20. private $_output = "<div";
  21. private $_data;
  22. public function construct($_attributes=array(), $data)
  23. {
  24. //扩展"instanceof"操作符的使用:嵌套(X)HTML widget
  25. if(!$data instanceof HTMLElement && !is_string($data)) {
  26. throw new Exception("data type error");
  27. }
  28. parent::construct($_attributes);
  29. $this->_data = $data;
  30. }
  31. public function getHTML()
  32. {
  33. foreach ($this->attributes as $key=>$val)
  34. {
  35. $this->_output.= " ".$key."='".$val."' ";
  36. }
  37. $this->_output =substr_replace($this->_output,">",-1);
  38. $this->_output .= $this->_data instanceof HTMLElement ? $this->_data->getHTML()."</div>" : $this->_data."</div>";
  39. return $this->_output;
  40. }
  41. }
  42. //定义具体的类"H1"扩展
  43. class h1 extends HTMLElement
  44. {
  45. private $_output="<h1";
  46. private $_data;
  47. public function construct($_attributes=array(), $data)
  48. {
  49. parent::construct($_attributes);
  50. $this->_data = $data;
  51. }
  52. public function getHTML()
  53. {
  54. foreach($this->attributes as $key=>$val)
  55. {
  56. $this->_output.= " ".$key."='".$val."' ";
  57. }
  58. $this->_output = substr_replace($this->_output, ">", -1);
  59. $this->_output .= $this->_data."<h1>";
  60. return $this->_output;
  61. }
  62. }
  63. //定义具体的类"ul"
  64. class ul extends HTMLElement
  65. {
  66. public $output = "<ul";
  67. private $ulitem=array();
  68. public function construct($_attributes=array(), $_ulitem=array())
  69. {
  70. parent::construct($_attributes);
  71. $this->ulitem = $_ulitem;
  72. }
  73. public function getHTML()
  74. {
  75. foreach($this->attributes as $key=>$val)
  76. {
  77. $this->_output.= " ".$key."='".$val."' ";
  78. }
  79. $this->output = substr_replace($this->output, ">",-1);
  80. foreach($this->ulitem as $ukey=>$uval){
  81. $this->output .="<li>".$uval."</li>";
  82. }
  83. $this->output.="</ul>";
  84. return $this->output;
  85. }
  86. }
  87. //生成页面的类
  88. class PageGenerator
  89. {
  90. private $_output;
  91. private $_title;
  92. public function construct($title=" Default page")
  93. {
  94. $this->_title = $title;
  95. }
  96. public function doHead()
  97. {
  98. $this->_output.="<html><head><title>".$this->_title."</title></head><body>";
  99. }
  100. // public function addHTMLElement($HTMLElement)
  101. // {
  102. // $this->_output.= $HTMLElement->getHTML();
  103. // }
  104. //对addHTMLElement进行改进
  105. //可以保证传入的不是HTMLElement类对像直接报错
  106. public function addHTMLElement($HTMLElement)
  107. {
  108. if(!$HTMLElement instanceof HTMLElement)
  109. {
  110. throw new Exception('Invalid (X)HTML element');
  111. }
  112. $this->_output.= $HTMLElement->getHTML();
  113. }
  114. public function doFooter()
  115. {
  116. $this->_output.="</body></html>";
  117. }
  118. public function fetchHTML()
  119. {
  120. return $this->_output;
  121. }
  122. }
  123. try{
  124. $attribute = array("class"=>"className", "style"=>"color:#000");
  125. $h1 = new H1($attribute, "h1内容");
  126. $attribute = array("class"=>"className", "style"=>"color:#000");
  127. $ul = new ul($attribute, array("li第一行内容","li第二行内容","li第三行内容"));
  128. $attribute = array("class"=>"className", "style"=>"color:red");
  129. $div = new Div($attribute, $ul);
  130. $page = new PageGenerator();
  131. // $str="我是个字符串";
  132. // $page->addHTMLElement($str);
  133. $page->addHTMLElement($h1);
  134. $page->addHTMLElement($div);
  135. // $page->addHTMLElement($ul);
  136. echo $page->fetchHTML();
  137. }
  138. catch(Exception $e){
  139. echo $e->getMessage();
  140. die();
  141. }
  142. ?>

以上就是php类型运算符"instanceof"操作符的扩展使用的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行