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>
...
...
@@ -138,33 +143,44 @@ text in child
138
143
<para>
139
144
When instantiating a custom <classname>DOMDocument</classname> the
140
145
<varname>ownerDocument</varname> property will refer to the instantiated
141
-
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
142
149
<function>DOMDocument::registerNodeClass</function> with
143
150
<classname>DOMDocument</classname>
144
151
</para>
145
152
<programlisting role="php">
146
153
<![CDATA[
147
154
<?php
148
-
class myDOMDocument extends DOMDocument {
155
+
class MyDOMDocument extends DOMDocument {
149
156
}
150
157

151
-
class myOtherDOMDocument extends DOMDocument {
158
+
class MyOtherDOMDocument extends DOMDocument {
152
159
}
153
160

154
-
// Create myDOMDocument with some XML
155
-
$doc = new myDOMDocument;
161
+
// Create MyDOMDocument with some XML
162
+
$doc = new MyDOMDocument;
156
163
$doc->loadXML("<root><element><child>text in child</child></element></root>");
157
164

158
165
$child = $doc->getElementsByTagName("child")->item(0);
159
166

160
-
// 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
161
172
var_dump(get_class($child->ownerDocument));
162
173

163
-
// Import a node from myDOMDocument
164
-
$newdoc = new myOtherDOMDocument;
174
+
// Import a node from MyDOMDocument
175
+
$newdoc = new MyOtherDOMDocument;
165
176
$child = $newdoc->importNode($child);
166
177

167
-
// 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
168
184
var_dump(get_class($child->ownerDocument));
169
185
?>
170
186
]]>
...
...
@@ -172,8 +188,50 @@ var_dump(get_class($child->ownerDocument));
172
188
&example.outputs;
173
189
<screen role="xml">
174
190
<![CDATA[
175
-
string(13) "myDOMDocument"
176
-
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
177
235
]]>
178
236
</screen>
179
237
</example>
...
...
@@ -181,7 +239,6 @@ string(18) "myOtherDOMDocument"
181
239
</refsect1>
182
240

183
241
</refentry>
184
-

185
242
<!-- Keep this comment at the end of the file
186
243
Local variables:
187
244
mode: sgml
188
245