There's a bug (similar reported 3 years ago as #28473 but tagged as "bogus" by lazy developer) in this function. When you eg. create own DOMElement class that have some properties and then try to set those properties on several nodes, only value set on the last node will be available.
<?php
class test extends DOMElement {
public $prop;
}
$doc = new DOMDocument();
$doc->registerNodeClass('DOMElement','test');
$doc->loadXML('<root><t1></t1><t2></t2></root>');
$tn = $doc->childNodes->item(0)->childNodes->item(0);
$tn->prop = 'test1'; // Set on T1 tag
$tn = $doc->childNodes->item(0)->childNodes->item(1);
$tn->prop = 'test2'; // Set on T2 tag
echo $doc->childNodes->item(0)->childNodes->item(0)->prop."<br>";
echo $doc->childNodes->item(0)->childNodes->item(1)->prop."<br>";
?>
Result: <br>test2<br>
Should be: test1<br>test2<br>
That and fact that one can't get unique PHP object identifcator (not XML attribute) of every node (also DOMText or DOMAttr) makes this PHP's DOMXML just a simple XML traverse function and not (as should be) powerful way to do custom (and FAST - there is no problem in rewriting DOM tree to own objects but where's in that the sense of using DOM anyway?) work with XML documents.
DOM implementation in PHP sucks and it would be better to not provide one (and make users create their own) than include that crap (and make users to think that they are able to do something in notime while it takes week).
DOMDocument->registerNodeClass()
(PHP 5 >= 5.2.0)
DOMDocument->registerNodeClass() — Register extended class used to create base node type
Descrição
This method allows you to register your own extended DOM class to be used afterward by the PHP DOM extension.
This method is not part of the DOM standard.
Parâmetros
- baseclass
-
The DOM class that you want to extend. You can find a list of these classes in the chapter introduction.
Of course, you won't be able to register a class extending DOMDocument but you can always start your document by instanciating your own extending class.
- extendedclass
-
Your extended class name. If NULL is provided, any previously registered class extending baseclass will be removed.
Valor Retornado
Retorna TRUE em caso de sucesso ou FALSE em falhas.
Histórico
| Versão | Descrição |
|---|---|
| PHP 5.2.2 | Prior to 5.2.2, a previously registered extendedclass had to be unregistered before being able to register a new class extending the same baseclass . |
Exemplos
Example#1 Adding a new method to DOMElement to ease our code
<?php
class myElement extends DOMElement {
function appendElement($name) {
return $this->appendChild(new myElement($name));
}
}
class myDocument extends DOMDocument {
function setRoot($name) {
return $this->appendChild(new myElement($name));
}
}
$doc = new myDocument();
$doc->registerNodeClass('DOMElement', 'myElement');
// From now on, adding an element to another costs only one method call !
$root = $doc->setRoot('root');
$child = $root->appendElement('child');
$child->setAttribute('foo', 'bar');
echo $doc->saveXML();
?>
O exemplo acima irá imprimir:
<?xml version="1.0"?> <root><child foo="bar"/></root>
DOMDocument->registerNodeClass()
07-Oct-2007 12:58
21-Sep-2007 09:07
If you want to implement your own DOMDocument class you have to register your new document class. Otherwise $node->ownerDocument will return an object of the type DOMDocument without your extra features.
I.e.:
public function __construct($version = "1.0", $encoding = "UTF-8")
{
parent::__construct($version, $encoding);
parent::registerNodeClass('DOMDocument', get_class($this));
}

DOMDocument->normalizeDocument()