aliasing and reference confusion....

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I hope that someone can set me straight on "alaising" and "references"  
in PHP....

I'm a C coder from ways back and this "new fangled" PHP way of doing  
things is hurting my brain.  :-)

OK, to set the background:

I have an XML document.  I am searching it using xpath.  So far so good.

Now I need to replace a node value.  In PHP4 and libxml 2.4, the  
node->set_value method doesn't actually set the value; it appends it to  
the node value.

So I've built a function setXpathNodeValue:

     function setXpathNodeValue($context, $pattern, $snode,  
&$new_content ) {

         if ($snode == '') $nodeset = xpath_eval($context, $pattern);
         else $nodeset = xpath_eval($context, $pattern, $snode);

         $arNodes = $nodeset->nodeset;
         $node = $arNodes[0];
         if ($node == NULL) return NULL;

         $dom = $node->owner_document();

         $newnode = $dom->create_element($node->tagname );

         $newnode->set_content($new_content );

         $atts = $node->attributes();
         foreach ($atts as $att ) {
             $newnode->set_attribute($att->name, $att->value );

         $kids = & $node->child_nodes();
         foreach ($kids as $kid ) {
             if ($kid->node_type() != XML_TEXT_NODE ) {
                 $newnode->append_child($kid );

         $node->replace_node($newnode );

(Yes, this comes almost verbatim from the docs.)

Except that I can't figure out how to "pass by reference" the $dom so  
that it is actually changed in the original.  That's the whole point of  
this exercise.

I guess I could just copy over the whole $dom, but that's potentially  

Any suggestions?  I am stuck....


Re: aliasing and reference confusion....

OK, got it figured out....

IN the calling routine:

setXpathNodeValue($dom, $context, $pattern, $snode, $new_content)

And in the function declaration:

setXpathNodeValue(& $dom, $context, $pattern, $snode, $new_content)

and then use $dom.  It works....


Site Timeline