DOMDocument::registerNodeClass

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

DOMDocument::registerNodeClassRegister extended class used to create base node type

Beschreibung

public DOMDocument::registerNodeClass(string $baseClass, ?string $extendedClass): bool

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.

Parameter-Liste

baseClass

The DOM class that you want to extend. You can find a list of these classes in the chapter introduction.

extendedClass

Your extended class name. If null is provided, any previously registered class extending baseClass will be removed.

Rückgabewerte

Gibt bei Erfolg true zurück. Bei einem Fehler wird false zurückgegeben.

Beispiele

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

?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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

Beispiel #2 Retrieving elements as custom class

<?php
class myElement extends DOMElement {
    public function 
__toString() {
        return 
$this->nodeValue;
    }
}

$doc = new DOMDocument;
$doc->loadXML("<root><element><child>text in child</child></element></root>");
$doc->registerNodeClass("DOMElement""myElement");

$element $doc->getElementsByTagName("child")->item(0);
var_dump(get_class($element));

// And take advantage of the __toString method..
echo $element;
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

string(9) "myElement"
text in child

Beispiel #3 Retrieving owner document

When instantiating a custom DOMDocument the ownerDocument property will refer to the instantiated class. However, if all references to that class are removed, it will be destroyed and new DOMDocument will be created instead. For that reason you might use DOMDocument::registerNodeClass() with DOMDocument

<?php
class MyDOMDocument extends DOMDocument {
}

class 
MyOtherDOMDocument extends DOMDocument {
}

// Create MyDOMDocument with some XML
$doc = new MyDOMDocument;
$doc->loadXML("<root><element><child>text in child</child></element></root>");

$child $doc->getElementsByTagName("child")->item(0);

// The current owner of the node is MyDOMDocument
var_dump(get_class($child->ownerDocument));
// MyDOMDocument is destroyed
unset($doc);
// And new DOMDocument instance is created
var_dump(get_class($child->ownerDocument));

// Import a node from MyDOMDocument
$newdoc = new MyOtherDOMDocument;
$child $newdoc->importNode($child);

// Register custom DOMDocument
$newdoc->registerNodeClass("DOMDocument""MyOtherDOMDocument");

var_dump(get_class($child->ownerDocument));
unset(
$doc);
// New MyOtherDOMDocument is created
var_dump(get_class($child->ownerDocument));
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

string(13) "MyDOMDocument"
string(11) "DOMDocument"
string(18) "MyOtherDOMDocument"
string(18) "MyOtherDOMDocument"

Beispiel #4 Custom objects are transient

Achtung

Objects of the registered node classes are transient, i.e. they are destroyed when they are no longer referenced from PHP code, and recreated when being retrieved again. That implies that custom property values will be lost after recreation.

<?php
class MyDOMElement extends DOMElement
{
    public 
$myProp 'default value';
}

$doc = new DOMDocument();
$doc->registerNodeClass('DOMElement''MyDOMElement');

$node $doc->createElement('a');
$node->myProp 'modified value';
$doc->appendChild($node);

echo 
$doc->childNodes[0]->myPropPHP_EOL;
unset(
$node);
echo 
$doc->childNodes[0]->myPropPHP_EOL;
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

modified value
default value

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Grundlagen von Views in MySQL

Views in einer MySQL-Datenbank bieten die Möglichkeit, eine virtuelle Tabelle basierend auf dem Ergebnis einer SQL-Abfrage zu erstellen. ...

admin

Autor : admin
Kategorie: mySQL-Tutorials

Definition von Stored Procedures - eine Einführung

Stored Procedures sind vordefinierte SQL-Codeblöcke, die in einer Datenbank gespeichert sind und bei Bedarf aufgerufen werden können. ...

Bernie

Autor : ebiz-consult GmbH & Co. KG
Kategorie: mySQL-Tutorials

Wie kann man komplexe Abfragen mit SQL-Querys In MySQLi effektiv durchführen?

In diesem MySQL-Tutorial wird erklärt, wie komplexe SQL-Abfragen in MySQLi effizient durchgeführt werden können. Wir werden uns mit verschiedenen Aspekten der Datenbankabfrage beschäftigen und spezifische Methoden kennenlernen. ...

TheMax

Autor : TheMax
Kategorie: mySQL-Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

Professioneller Webentwickler & Webdesigner

Of course, here is the translation: Hello, Thank you for your interest in the long-term project. Your extensive skills and experience in web dev ...

Geschrieben von Athelstan am 15.04.2024 09:25:39
Forum: Jobgesuche
Wir stellen unsere SEO-Agentur vor

Hallo In der heutigen digitalen Welt war es für Unternehmen noch nie so einfach, ihre Reichweite weltweit zu vergrößern. Wenn Sie außerhalb I ...

Geschrieben von thomasmuller am 14.04.2024 07:18:33
Forum: User stellen sich vor
Spielplan für 4 Gruppen zu je 6 Teams auf 2 Feldern

Hallöchen zusammen, ich versuche derzeit unseren Excel-Spielplan in PHP zu überführen. Eigentlich bin ich auch shon fertig - wenn da nicht dies ...

Geschrieben von derH0st am 11.04.2024 15:58:37
Forum: PHP Developer Forum
PHP 8.3.3 - App kann auf dem PC nicht ausgeführt werden

Problem gelöst. Die php.exe hatte einen defekt. Neue heruntergeladen und fertig.

Geschrieben von Tetra1081 am 10.04.2024 16:49:14
Forum: PHP Developer Forum