Hi there, I have been trying to get my head around how simplexml and dom updates XML between functions. The reason i ask is that the code i have written seems to work, but without me having to declare anything as global, and im a little confused as to why it does this.
For example, i have this (simplified) code:
<?
foreach ($filenames as $filename) {
$xml = simplexml_load_file($filename);
updateXml($xml);
$xml->last_update = date('Y-m-d H:i:s');
$xml->asXML($filename);
}
function updateXml($xml) {
//...
if ($data = $xml->xpath('//data/info[product_id="' . $product_data['id'] . '"]')) {
$parent = $data[0]->xpath("parent::*");
$data = updateItem($parent[0], $product_data);
} else {
$product = addItem($xml->products, $product_data['id']);
$data = updateItem($product, $product_data);
}
}
function updateItem($parent, $product_data) {
$node = dom_import_simplexml($parent);
$dom = $node->ownerDocument;
$product = $dom->createElement('product');
$node->appendChild($product);
$item = $dom->createElement('id', $product_data['id']);
$product->appendChild($item);
$item = $dom->createElement('name', $product_data['name']);
$product->appendChild($item);
$item = $dom->createElement('url');
$product->appendChild($item);
$cdata = $dom->createCDATASection($product_data['url']);
$item->appendChild($cdata);
$item = $dom->createElement('price', $product_data['price']);
$product->appendChild($item);
return $node;
}
?>
Can you please help me understand how the XML is being updated between the functions without needing to declare it as global? I know it seems strange to ask about something that works, but i need to get my head around it :-)
Thanks