Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何扩展PHP DomeElement?_Php_Dom_Inheritance - Fatal编程技术网

如何扩展PHP DomeElement?

如何扩展PHP DomeElement?,php,dom,inheritance,Php,Dom,Inheritance,a、 php 从文件中复制和粘贴 在搜索了如何扩展 DOMDocument和DOMElement我发现了一个 错误中的方法: . 以下代码显示了如何: #!/usr/bin/php <?php class HtmlTable extends DOMElement { public function __construct($height, $width) { parent::__construct("table"

a、 php

从文件中复制和粘贴

在搜索了如何扩展 DOMDocument和DOMElement我发现了一个 错误中的方法: . 以下代码显示了如何:


#!/usr/bin/php
<?php
class HtmlTable extends DOMElement
{   
        public function __construct($height, $width)
        {   
                parent::__construct("table");
                for ($i = 0; $i < $height; ++$i) {
                        $row = $this->appendChild(new DOMElement("tr"));
                        for($j = 0; $j < $width; ++$j) {
                                $row->appendChild(new DOMElement("td"));
                        }   
                }   
        }   
}   

$document = new DOMDocument("1.0", "UTF-8");
$document->registerNodeClass("DOMElement", "HtmlTable");
$document->appendChild(new HtmlTable(3, 2));
$document->saveXML();
Fatal error: Uncaught exception 'DOMException' with message 'No Modification Allowed Error' in /home/www/a.php:9
Stack trace:
#0 /home/www/a.php(9): DOMNode->appendChild(Object(DOMElement))
#1 /home/www/a.php(19): HtmlTable->__construct(3, 2)
#2 {main}
  thrown in /home/www/a.php on line 9
<?php
class extDOMDocument extends DOMDocument {
 public function createElement($name, $value=null) {
  $orphan = new extDOMElement($name, $value); // new  sub-class object
  $docFragment = $this->createDocumentFragment(); // lightweight container maintains "ownerDocument"
  $docFragment->appendChild($orphan); // attach
  $ret = $docFragment->removeChild($orphan); // remove
  return $ret; // ownerDocument set; won't be destroyed on  method exit
 }
 // .. more class definition
}

class extDOMElement extends DOMElement {
 function __construct($name, $value='', $namespaceURI=null) {
  parent::__construct($name, $value, $namespaceURI);
 }
  //  ... more class definition here
}

$doc = new extDOMDocument('test');
$el = $doc->createElement('tagname');
$el->setAttribute("attr", "val");
$doc->appendChild($el);

// append discards the DOMDocumentFragment and just adds its child nodes, but ownerDocument is maintained.
echo get_class($el)."<br/>";
echo get_class($doc->documentElement)."<br/>";
echo "<xmp>".$doc->saveXML()."</xmp>";
?>