The "instanceof" operator is a good feature of type checking for input objects that are injected directly into the Page builder class. Now, let's add a check routine to the constructor of the (X) HTML widget class and the "gethtml ()" method so that they can accept other widgets as input parameters. Please check the following improved classes:
Class Div extends htmlelement{private $output = ' <div ', private $data, public function construct ($attributes =array (), $dat A) {if (! $data instanceof htmlelement&&!is_string ($data)) {throw new Exception (' Invalid parameter type '); } parent::construct ($attributes); $this->data= $data; The concrete implementation of the//' gethtml () ' method is public function gethtml () {foreach ($this->attributes as $attribute => $value) {$this->output.= $at Tribute. ' = "'. $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 = '
As shown in the above class, to allow nested (X) HTML elements to be implemented when the corresponding Web page is generated, their constructors and the "gethtml ()" Method are refactored respectively. Note that the following conditional block is included in the constructor for each class:
if (! $data instanceof htmlelement&&!is_string ($data)) {throw new Exception (' Invalid parameter type ');}
At this point, what is actually done is to make sure that only the string data and the "HtmlElement" type object are allowed as input parameters for each class. Otherwise, an exception is thrown by the respective method, respectively, and may cause the application to stop executing. So, this is the process of checking the input data. Now, let's take a look at the new signature of the "gethtml ()" method, which also uses the "instanceof" operator:
$this->output.= ($this->data instanceof htmlelement)? $this->data->gethtml (): $this->data;
As you can see, in this case, it is useful for the this operator to take advantage of the polymorphism characteristics of the (X) HTML widget class. If the $data property is also a widget, its "gethtml ()" method is called correctly, which results in the display of nested page elements. On the other hand, if it is just a string, it is added directly to all the output of the current class.
Now, to ensure that some objects belong to a particular type, you may have understood the use of the "instanceof" operator in PHP 5. As you can see in this article, forcing the object type in PHP 5 is actually a fairly straightforward task. Now, you'd better develop an example of using this method to filter objects in your PHP application to deepen your understanding.
<?php//defines a htmlelementabstract class htmlelement{protected $attributes; protected function construct ($_attributes) {if (!is_array ($_attributes)) {throw new Exception ("Attributes not an IS array"); } $this->attributes = $_attributes; }//define a virtual function gethtml ();} Define the specific class "div" extension htmlelementclass Div extends htmlelement{private $_output = "<div"; Private $_data; Public Function construct ($_attributes=array (), $data) {//Extend the use of the "instanceof" operator: nested (X) HTML widget if (! $dat A instanceof htmlelement &&!is_string ($data)) {throw new Exception ("Data type error"); } parent::construct ($_attributes); $this->_data = $data; } public Function gethtml () {foreach ($this->attributes as $key + $val) {$this _output.= "". $key. " = ' ". $val." ' "; } $this->_output =substr_replace ($this->_output, ">",-1); $this->_output. = $this->_data instanceof htmlelement? $this->_data->gethtml (). " </div> ": $this->_data." </div> "; return $this->_output; }}//define the specific class "H1" extension class H1 extends htmlelement{private $_output= "<H1"; Private $_data; Public Function construct ($_attributes=array (), $data) {parent::construct ($_attributes); $this->_data = $data; The Public Function gethtml () {foreach ($this->attributes as $key = = $val) {$this-& Gt;_output.= "". $key. " = ' ". $val." ' "; } $this->_output = Substr_replace ($this->_output, ">",-1); $this->_output. = $this->_data. "