DOMElement::setAttribute() manual example question

From: Date: Fri, 03 Mar 2006 11:55:09 +0000
Subject: DOMElement::setAttribute() manual example question
Groups: php.general 
Request: Send a blank email to php-general+get-231398@lists.php.net to get a copy of this message
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

« previous php.general (#231398) next »