php dom结构,PHP DOMImplementation 用法 手册 | 示例代码

Ok got it working like a charm using "proxy pattern" with traits. The idea being declaring the common methods inside a "trait" in order for extended and registered Node Classes to have access even if not derived / child of the extended DOMNode…

Here a small snippet :

traittNode{// We need the magic method __get in order to add properties such as DOMNode->parentElementpublic function__get($name)

{    if(property_exists($this,$name)){return$this->$name;}

if(method_exists($this,$name)){return$this->$name();}

throw newErrorException('my\Node property ''.(string)$name.'' not found…',42,E_USER_WARNING);

}// parentElement property definitionprivate functionparentElement()

{    if($this->parentNode===null){returnnull;}

if($this->parentNode->nodeType===XML_ELEMENT_NODE){return$this->parentNode;}

return$this->parentNode->parentElement();

}// JavaScript equivalentpublic functionisEqualNode(DOMNode $node){return$this->isSameNode($node);}

public functioncompareDocumentPosition(DOMNode $otherNode)

{    if($this->ownerDocument!==$otherNode->ownerDocument){returnDOCUMENT_POSITION_DISCONNECTED;}$c=strcmp($this->getNodePath(),$otherNode->getNodePath());

if($c===0){return0;}

else if($c<0){returnDOCUMENT_POSITION_FOLLOWING($c< -1?DOCUMENT_POSITION_CONTAINED_BY:0);}

returnDOCUMENT_POSITION_PRECEDING($c>1?DOCUMENT_POSITION_CONTAINS:0);

}

public functioncontains(DOMNode $otherNode){return ($this->compareDocumentPosition($otherNode) >=DOCUMENT_POSITION_CONTAINED_BY);}

}

classDocumentextendsDomDocument{    public function__construct($version=null,$encoding=null)

{parent::__construct($version,$encoding);$this->registerNodeClass('DOMNode','myNode');$this->registerNodeClass('DOMElement','myElement');$this->registerNodeClass('DOMDocument','myDocument');/* [...] */}

}

classElementextendsDOMElement{    usetNode;/* [...] */}

classNodeextendsDOMNode{    usetNode;/* [...] */}?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值