扩展 DOMElement 对象在导入到另一个文档时会丢失其属性
当将具有特定属性的扩展 DOMElement 对象导入到另一个 DOMDocument 中时,它创建的所有属性都会丢失(我猜它实际上并没有复制 no ,而是为另一个文档创建了一个新节点,并且只是为DOMElement 类被复制到新节点)。让属性在导入的元素中仍然可用的最佳方法是什么?
这是问题的示例:
<?php
class DOMExtendedElement extends DOMElement {
private $itsVerySpecialProperty;
public function setVerySpecialProperty($property) {$this->itsVerySpecialProperty = $property;}
}
// First document
$firstDocument = new DOMDocument();
$firstDocument->registerNodeClass("DOMElement", "DOMExtendedElement");
$elm = $firstDocument->createElement("elm");
$elm->setVerySpecialProperty("Hello World!");
var_dump($elm);
// Second document
$secondDocument = new DOMDocument();
var_dump($secondDocument->importNode($elm, true)); // The imported element is a DOMElement and doesn't have any other properties at all
// Third document
$thirdDocument = new DOMDocument();
$thirdDocument->registerNodeClass("DOMElement", "DOMExtendedElement");
var_dump($thirdDocument->importNode($elm, true)); // The imported element is a DOMExtendedElement and does have the extra property but it's empty
?>
When importing an extended DOMElement object with specific properties into another DOMDocument than the one it was created with all properties are lost (I guess it doesn't actually copy the no but a new node is created for the other document and just the values for the DOMElement class are copied to the new node). What would be the best way to have the properties still available in the imported element?
Here's an example of the problem:
<?php
class DOMExtendedElement extends DOMElement {
private $itsVerySpecialProperty;
public function setVerySpecialProperty($property) {$this->itsVerySpecialProperty = $property;}
}
// First document
$firstDocument = new DOMDocument();
$firstDocument->registerNodeClass("DOMElement", "DOMExtendedElement");
$elm = $firstDocument->createElement("elm");
$elm->setVerySpecialProperty("Hello World!");
var_dump($elm);
// Second document
$secondDocument = new DOMDocument();
var_dump($secondDocument->importNode($elm, true)); // The imported element is a DOMElement and doesn't have any other properties at all
// Third document
$thirdDocument = new DOMDocument();
$thirdDocument->registerNodeClass("DOMElement", "DOMExtendedElement");
var_dump($thirdDocument->importNode($elm, true)); // The imported element is a DOMExtendedElement and does have the extra property but it's empty
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它可能有更好的解决方案,但您可能需要
克隆
第一个对象结果:
此处演示
It may have a better solution but you may need to
clone
the first objectResult :
Demo here