|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-05-21 09:38 UTC] php at xxlwebdesign dot de
Description:
------------
The method DOMDocument->registerNodeClass does not work with DOMNodeList
Reproduce code:
---------------
<?php
class MyNodeList extends DOMNodeList
{
}
$dom = new DOMDocument();
$dom->registerNodeClass('DOMNodeList', 'MyNodeList');
?>
Actual result:
--------------
Fatal error: DOMDocument::registerNodeClass() [<a href='domdocument.registernodeclass'>domdocument.registernodeclass</a>]: Class DOMNodeList is not derived from DOMNode. in ...
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 09:00:01 2025 UTC |
I’d like to provide a valid use-case for this bug. I have created an extended DOMDocument that upon __construct also creates an XPath object, that way I can add methods to DOMElement to allow for DOMElement->xpath ('...') instead of using DOMXPath->query ('...', DOMNode). This makes the DOM more chainable and simpler to use without so many temporary variables. What I want to do is extend DOMNodeList so that one function call can apply to all nodes returned from an XPath query, e.g. DOMNode->xpath ('a')->addClass ('link'); Is anybody aware of any other work around that can be used to achieve this same effect?@kroccamen While not that elegant, you can create a decorator to solve this issue. Here's a template to use: class MyDomElement extends DOMElement { function xpath($query) { return new DomNodeListDecorator($this->ownerDocument->getXpathSelector()- >query($query, $this)); } } class DomNodeListDecorator implements IteratorAggregate { protected $nodeList; function __construct($nodeList) { $this->nodeList = $nodeList; } function getIterator() { return $this->nodeList; } function __get($name) { if ($name == 'length') { return $this->nodeList->length; } throw new Exception("Undefined attribute {$name}"); } function item($index) { return $this->nodeList->item($index); } }