SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simpleXML variables... how is it possible?

    With simpleXML variables can be both an object and an array.

    For example, you can have $node['attribute'] and $node->childNode at the same time. How is that even possible?

    I've tried but can't get PHP on its own to allow a variable to be more than one datatype. Must be something in the extension?

  2. #2
    SitePoint Member
    Join Date
    Feb 2005
    Location
    fr
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A class can implement the ArrayAccess interface which has four methods :

    http://www.php.net/~helly/php/ext/sp...rayAccess.html

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At one point you could array_keys() on an object. I'm not sure if that's still valid syntax ?
    As far as I understand, both object and array are implemented as a hashmap in the zend-engine.

  4. #4
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I could be wrong, but I'm not sure what the Array Access has to do with it. If you make a simpleXML object with XML that has attributes, and then dump out the object, the attributes are not part of the object at all, but they are part of an array that has the same variable name as the object.

    It is bizzare. watch:

    Code:
    % php -a                                             
    Interactive mode enabled
    
    php > $xml_string = <<<XML
    <<< > <root>
    <<< > <node type="one">node text</node>
    <<< > <node type="two">other</node>
    <<< > <node>n/a</node>
    <<< > </root>
    <<< > XML;
    php > 
    php > $xml = simplexml_load_string($xml_string);
    php > 
    php > var_dump($xml);
    object(SimpleXMLElement)#1 (1) {
      ["node"]=>
      array(3) {
        [0]=>
        string(9) "node text"
        [1]=>
        string(5) "other"
        [2]=>
        string(3) "n/a"
      }
    }
    php >
    The object is a SimpleXMLElement, which I knew, but the odd part is that it appears to contain no information about the attributes at all. But then again it does:

    Code:
    php > echo $xml->node[0];
    node text
    php > echo $xml->node[0]['type'];         
    one
    php >
    Where in the world is $xml->node[0]['type'] coming from, and how in world can both $xml->node[0] and $xml->node[0]['type'] reference a string value?

    If you try to do that in PHP, you get some funky results because one partially overwrites the other:
    Code:
    php > $node[0] = 'string';
    php > $node[0]['type'] = 'another string';
    php > echo $node[0];
    atring
    php > echo $node[0]['type'];
    a
    php >
    Serialize doesn't even know about the attributes:
    Code:
    php > echo serialize($xml);
    O:16:"SimpleXMLElement":1:{s:4:"node";a:3:{i:0;s:9:"node text";i:1;s:5:"other";i:2;s:3:"n/a";}}
    php >
    And it also looks like you cannot unserialize an SimpleXMLElement object, which I just discovered now, and was immediately bummed about:
    Code:
    php > $xmlCopy = unserialize(serialize($xml));
    PHP Warning:  unserialize(): Node no longer exists in php shell code on line 1
    
    Warning: unserialize(): Node no longer exists in php shell code on line 1
    php > var_dump($xmlCopy);
    PHP Warning:  var_dump(): Node no longer exists in php shell code on line 1
    
    Warning: var_dump(): Node no longer exists in php shell code on line 1
    object(SimpleXMLElement)#4 (0) {
    }
    php >
    I'm trying to understand how SimpleXML works, but even PHP doesn't seem to know... can anyone shed any light on the bizarre variable constructs of SimpleXML?

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dreamscape
    Where in the world is $xml->node[0]['type'] coming from, and how in world can both $xml->node[0] and $xml->node[0]['type'] reference a string value?
    http://us3.php.net/manual/en/functio...attributes.php
    Note: SimpleXML has made a rule of adding iterative properties to most methods. They cannot be viewed using var_dump() or anything else which can examine objects.
    http://us3.php.net/manual/en/function.serialize.php

    It is not possible to serialize PHP built-in objects


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •