reference/dom/domdocument/registernodeclass.xml
bb47f4433ac38d890373169666e82609d2ab0848
...
...
@@ -7,10 +7,10 @@
7
7
</refnamediv>
8
8
<refsect1 role="description">
9
9
&reftitle.description;
10
-
<methodsynopsis>
10
+
<methodsynopsis role="DOMDocument">
11
11
<modifier>public</modifier> <type>bool</type><methodname>DOMDocument::registerNodeClass</methodname>
12
-
<methodparam><type>string</type><parameter>baseclass</parameter></methodparam>
13
-
<methodparam><type>string</type><parameter>extendedclass</parameter></methodparam>
12
+
<methodparam><type>string</type><parameter>baseClass</parameter></methodparam>
13
+
<methodparam><type class="union"><type>string</type><type>null</type></type><parameter>extendedClass</parameter></methodparam>
14
14
</methodsynopsis>
15
15

16
16
<para>
...
...
@@ -20,6 +20,11 @@
20
20
<para>
21
21
This method is not part of the DOM standard.
22
22
</para>
23
+
<caution>
24
+
<simpara>
25
+
The constructor of the objects of the registered node classes is not called.
26
+
</simpara>
27
+
</caution>
23
28
</refsect1>
24
29

25
30
<refsect1 role="parameters">
...
...
@@ -27,7 +32,7 @@
27
32
<para>
28
33
<variablelist>
29
34
<varlistentry>
30
-
<term><parameter>baseclass</parameter></term>
35
+
<term><parameter>baseClass</parameter></term>
31
36
<listitem>
32
37
<para>
33
38
The DOM class that you want to extend. You can find a list of these
...
...
@@ -36,11 +41,11 @@
36
41
</listitem>
37
42
</varlistentry>
38
43
<varlistentry>
39
-
<term><parameter>extendedclass</parameter></term>
44
+
<term><parameter>extendedClass</parameter></term>
40
45
<listitem>
41
46
<para>
42
47
Your extended class name. If &null; is provided, any previously
43
-
registered class extending <parameter>baseclass</parameter> will
48
+
registered class extending <parameter>baseClass</parameter> will
44
49
be removed.
45
50
</para>
46
51
</listitem>
...
...
@@ -56,40 +61,6 @@
56
61
</para>
57
62
</refsect1>
58
63

59
-
<refsect1 role="changelog">
60
-
&reftitle.changelog;
61
-
<para>
62
-
<informaltable>
63
-
<tgroup cols="2">
64
-
<thead>
65
-
<row>
66
-
<entry>&Version;</entry>
67
-
<entry>&Description;</entry>
68
-
</row>
69
-
</thead>
70
-
<tbody>
71
-
<row>
72
-
<entry>5.2.2</entry>
73
-
<entry>
74
-
Prior to 5.2.2, a previously registered <parameter>extendedclass</parameter>
75
-
had to be unregistered before being able to register a new class extending
76
-
the same <parameter>baseclass</parameter>.
77
-
</entry>
78
-
</row>
79
-
<!--
80
-
<row>
81
-
<entry>...</entry>
82
-
<entry>
83
-
...
84
-
</entry>
85
-
</row>
86
-
-->
87
-
</tbody>
88
-
</tgroup>
89
-
</informaltable>
90
-
</para>
91
-
</refsect1>
92
-

93
64
<refsect1 role="examples">
94
65
&reftitle.examples;
95
66
<para>
...
...
@@ -172,33 +143,44 @@ text in child
172
143
<para>
173
144
When instantiating a custom <classname>DOMDocument</classname> the
174
145
<varname>ownerDocument</varname> property will refer to the instantiated
175
-
class, meaning there is no need (and in fact not possible) to use
146
+
class. However, if all references to that class are removed, it
147
+
will be destroyed and new <classname>DOMDocument</classname> will be
148
+
created instead. For that reason you might use
176
149
<function>DOMDocument::registerNodeClass</function> with
177
150
<classname>DOMDocument</classname>
178
151
</para>
179
152
<programlisting role="php">
180
153
<![CDATA[
181
154
<?php
182
-
class myDOMDocument extends DOMDocument {
155
+
class MyDOMDocument extends DOMDocument {
183
156
}
184
157

185
-
class myOtherDOMDocument extends DOMDocument {
158
+
class MyOtherDOMDocument extends DOMDocument {
186
159
}
187
160

188
-
// Create myDOMDocument with some XML
189
-
$doc = new myDOMDocument;
161
+
// Create MyDOMDocument with some XML
162
+
$doc = new MyDOMDocument;
190
163
$doc->loadXML("<root><element><child>text in child</child></element></root>");
191
164

192
165
$child = $doc->getElementsByTagName("child")->item(0);
193
166

194
-
// The current owner of the node is myDOMDocument
167
+
// The current owner of the node is MyDOMDocument
168
+
var_dump(get_class($child->ownerDocument));
169
+
// MyDOMDocument is destroyed
170
+
unset($doc);
171
+
// And new DOMDocument instance is created
195
172
var_dump(get_class($child->ownerDocument));
196
173

197
-
// Import a node from myDOMDocument
198
-
$newdoc = new myOtherDOMDocument;
174
+
// Import a node from MyDOMDocument
175
+
$newdoc = new MyOtherDOMDocument;
199
176
$child = $newdoc->importNode($child);
200
177

201
-
// The new owner of the node has changed to myOtherDOMDocument
178
+
// Register custom DOMDocument
179
+
$newdoc->registerNodeClass("DOMDocument", "MyOtherDOMDocument");
180
+

181
+
var_dump(get_class($child->ownerDocument));
182
+
unset($doc);
183
+
// New MyOtherDOMDocument is created
202
184
var_dump(get_class($child->ownerDocument));
203
185
?>
204
186
]]>
...
...
@@ -206,8 +188,50 @@ var_dump(get_class($child->ownerDocument));
206
188
&example.outputs;
207
189
<screen role="xml">
208
190
<![CDATA[
209
-
string(13) "myDOMDocument"
210
-
string(18) "myOtherDOMDocument"
191
+
string(13) "MyDOMDocument"
192
+
string(11) "DOMDocument"
193
+
string(18) "MyOtherDOMDocument"
194
+
string(18) "MyOtherDOMDocument"
195
+
]]>
196
+
</screen>
197
+
</example>
198
+
</para>
199
+
<para>
200
+
<example xml:id="domdocument.registernodeclass.example.transient">
201
+
<title>Custom objects are transient</title>
202
+
<caution>
203
+
<simpara>
204
+
Objects of the registered node classes are transient, i.e. they are
205
+
destroyed when they are no longer referenced from PHP code, and recreated
206
+
when being retrieved again. That implies that custom property values will be
207
+
lost after recreation.
208
+
</simpara>
209
+
</caution>
210
+
<programlisting role="php">
211
+
<![CDATA[
212
+
<?php
213
+
class MyDOMElement extends DOMElement
214
+
{
215
+
public $myProp = 'default value';
216
+
}
217
+

218
+
$doc = new DOMDocument();
219
+
$doc->registerNodeClass('DOMElement', 'MyDOMElement');
220
+

221
+
$node = $doc->createElement('a');
222
+
$node->myProp = 'modified value';
223
+
$doc->appendChild($node);
224
+

225
+
echo $doc->childNodes[0]->myProp, PHP_EOL;
226
+
unset($node);
227
+
echo $doc->childNodes[0]->myProp, PHP_EOL;
228
+
?>]]>
229
+
</programlisting>
230
+
&example.outputs;
231
+
<screen role="xml">
232
+
<![CDATA[
233
+
modified value
234
+
default value
211
235
]]>
212
236
</screen>
213
237
</example>
...
...
@@ -215,7 +239,6 @@ string(18) "myOtherDOMDocument"
215
239
</refsect1>
216
240

217
241
</refentry>
218
-

219
242
<!-- Keep this comment at the end of the file
220
243
Local variables:
221
244
mode: sgml
222
245