Forum und email

DOMDocument->registerNodeClass()

(PHP 5 >= 5.2.0)

DOMDocument->registerNodeClass() — Register extended class used to create base node type

설명

DOMDocument
bool registerNodeClass ( string $baseclass , string $extendedclass )

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.

매개변수

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.

반환값

성공할 경우 TRUE를, 실패할 경우 FALSE를 반환합니다.

변경 기록

버전 설명
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 .

예제

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();

?>

위 예제의 출력:

<?xml version="1.0"?>
<root><child foo="bar"/></root>