2017-01-23 88 views
5

在PHP中扩展DOMElement时,未调用子类的构造函数。就预期的行为而言,文档中没有任何内容跳出来,但也许我错过了一些东西。这里有一个简单的测试用例....未在扩展PHP DOMElement上调用的构造函数

class SillyTestClass extends DOMElement{ 
    public $foo=null; 
    public function __construct($name,$value=null,$namespace=null){ 
     echo "calling custom construct...."; 
     $this->foo="bar"; 
     parent::__construct($name,$value,$namespace); 
    } 
    public function sayHello(){ 
     echo "Why, hello there!"; 
    } 
} 

$doc=new DOMDocument(); 
$doc->registerNodeClass('DOMElement','SillyTestClass'); 
$doc->loadHTML("<div><h1>Sample</h1></div>"); 
//THIS WORKS! CUSTOM CLASS BEING USED 
$doc->documentElement->firstChild->sayHello(); 

//THIS IS STILL NULL:(Never set by construct, no message saying construct was called either 
echo $doc->documentElement->firstChild->foo; 

当然,如果我实例它自己还行吧......

$elm=new SillyTestClass("foo","Hi there"); 
//WORKS! Outputs "bar"; 
echo $elm->foo; 

为什么当我与DOM文档注册节点类它不叫__construct尽管它以其他方式给我适当的继承?

UPDATE对于真正好奇的人或谁懂C

人============================= ========================================== 调查....

这是 PHP src on github

如果您要创建一个元素所采取的DOM扩展源,这是发生的连锁事件::

document.c :: dom_document_create_element 
    | //uses libxml to generate a new DOMNode 
    | node = xmlNewDocNode(docp, NULL, (xmlChar *) name, (xmlChar *) value); 

    // that node is then sent to 
    php_dom.c :: php_dom_create_object 
    | 
    | //the node type is used to figure out what extension class to use 
    |  switch (obj->type) {... 
    |  
    |  //that class is used to instance an object 
    |  if (domobj && domobj->document) { 
    |   ce = dom_get_doc_classmap(domobj->document, ce); 
    |  } 
     object_init_ex(return_value, ce); 

看来,你不要从延伸的DOMNode获得真正的继承,或者它内置的扩展类(一个DOMElement,一个DOMText)如果DOM文档实例他们。在这种情况下,首先创建libxml节点,然后再次添加我们的类属性。

这是不幸的,不可能绕过它似乎,因为即使当您将导入节点到文档中时,它实例化新节点。例如

class extendsDE extends DOMElement{ 
    public $constructWasCalled=false; 
    public function __construct($name){ 
     parent::__construct($name); 
     $this->constructWasCalled=true; 
    } 
} 

class extendsDD extends DOMDocument{ 

    public function __construct(){ 
     parent::__construct(); 
     $this->registerNodeClass("DOMElement","extendsDE"); 
    } 
    //@override 
    public function createElement($name){ 
     $elm=new extendsDE($name); 
     echo "Element construct called when we create="; 
     echo $elm->constructWasCalled?"true":"false"; 
     return $this->importNode($elm); 
    } 
} 

$doc=new extendsDD(); 
$node=$doc->createElement("div"); 
echo "<br/>"; 
echo "But then when we import into document, a new element is created and construct called= "; 
echo $node->constructWasCalled?"true":"false"; 

现在debate-这是开发商打算什么和文档是误导性的,或者是一个错误,真正的继承是应该发生的呢?

+0

甚至在文档中还有一个注释,表明构造函数应该调用'parent :: __ construct()'。但是我得到和你一样的结果。 – Barmar

+0

哪个版本的PHP你出于好奇使用? – user2782001

+0

我用PHP 5.5.38 – Barmar

回答

1

到目前为止,我在某些情况下已经找到了解决这个问题的方法。 DOMNode被保存在内存中,所以只要你可以将它整合到文档中(你的构造函数叫做),那么无论你用它做什么或者你如何访问它,它都可以。

下面是一个例子,我们可以得到的结构打电话,仍然是好的与

class extendsDE extends DOMElement{ 
    public $constructWasCalled=false; 
    public function __construct($name){ 
     parent::__construct($name); 
      $this->constructWasCalled=true; 
    } 
    } 

    $doc=new DOMDocument(); 
    $doc->registerNodeClass("DOMElement","extendsDE"); 
    $doc->loadHTML("<div></div>"); 

    //append a node we create manually rather than through createElement 
    $node=$doc->getElementsByTagName('div')->item(0)->appendChild(new extendsDE("p")); 
    $node->nodeValue="Was my construct called?"; 
    echo "<br/>"; 
    echo "A new element was appended and construct called= "; 
    echo $node->constructWasCalled?"true":"false"; 
    echo "<br/>"; 
    echo "Okay but what happens if I retrieve that node some other way.."; 
    echo "<br/>"; 
    echo "what if I get that element through selectors. Custom property still set from constructor="; 
    echo $doc->getElementsByTagName('p')->item(0)->constructWasCalled?"true":"false"; 
    echo "<br/>"; 
    echo "what if I get that element through relationships. Custom property still set from constructor="; 
    echo $doc->getElementsByTagName('div')->item(0)->childNodes->item(0)->constructWasCalled?"true":"false"; 

文档美中不足的是:

,只有当您创建的所有元素的作品。如果使用$ document-> loadHTML($ html)加载HTML标记,则不会调用您的扩展构造函数。到目前为止,我只能想到如何加载标记,然后将每个节点循环到实例副本并插入它们。肯定可能,但是很慢......