开发者

PHP SimpleXML DOM using between functions

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'] . '"]')) {
  开发者_JS百科      $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


You are passing a copy of the instance of an object through each of your functions.

You can think of parameter passing as sharing the same instance of an object to each of your functions, opposed to declaring it global.


You think you're only passing the nodes that you stored in $product (or other var): you're not. You're passing the original object, with some parameters set about a selected nodeset.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜