Hi!
I've a question regarding the example in DOMElement::setAttribute()
chapter of the PHP manual: http://de3.php.net/dom-domelement-setattribute
There, an attribute is added to an element this way:
<?php
$doc = new DOMDocument("1.0");
$node = $doc->createElement("root");
$newnode = $doc->appendChild($node);
$newnode->setAttribute("align", "left");
echo $doc->saveXML();
?>
$doc->createElement() returns the created DOMElement,
$doc->appendChild() returns the appended DOMNode. Isn't this the same
object? Is it a reference?
I'm asking, because the following works too:
<?php
$doc = new DOMDocument("1.0");
$node = $doc->createElement("root");
$node->setAttribute("align", "left");
$doc->appendChild($node);
echo $doc->saveXML();
?>
I like the 2nd example more, because first you create an object
(DOMElement), add some attributes to the object and after that append it
somewhere to the DOM tree. The 1st example creates a new DOMElement
object, appends it to the DOM tree, and adds the attributes not to the
created object, but to another object (the DOMNode appended to the
tree), which seems to be a reference to the original object.
Why does the manual prefer the (IMO less intuitive) 1st way? Is there a
problem with the 2nd way?
best regards
Andreas
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php