Let's say I want to append a div to a DOMDocument. I can do so with:
<?php
$dom = new DOMDocument();
$dom->appendChild(
$dom->createElement("div")
);
Now, say I want to add some text to that div, so I try:
<?php
$dom = new DOMDocument();
$dom->appendChild(
$dom->createElement("div")
->appendChild( $dom->createTextNode("foobar") )
);
But wait! Now there is a problem!
In the first case, $dom->createElement("div")
returned an empty "div" DOMNode, which appendChild() had no problem accepting.
But in the second case, $dom->createElement("div")->appendChild($dom->createTextNode("foobar"))
returns the already appended "foobar" DOMText. So the "div" DOMNode does not get appended, and php throws a warning.
Warning: DOMNode::appendChild(): Couldn't fetch DOMText
My question is, is there a way to get the method chain to return the original modified (with the DOMText appended) DOMNode that is returned by createElement()?
I know I could just save the DOMNode to a variable and then pass it to appendChild() but I would really love to see a one liner solution.
Thanks.
Fiddle: http://codepad.org/PFB3Ns7E