在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
人============================= ========================================== 调查....
如果您要创建一个元素所采取的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-这是开发商打算什么和文档是误导性的,或者是一个错误,真正的继承是应该发生的呢?
甚至在文档中还有一个注释,表明构造函数应该调用'parent :: __ construct()'。但是我得到和你一样的结果。 – Barmar
哪个版本的PHP你出于好奇使用? – user2782001
我用PHP 5.5.38 – Barmar