#43542 [Opn]: simpleXML thinks that comment is node
ID: 43542 Updated by: [EMAIL PROTECTED] Reported By: 007not at gmail dot com Status: Open Bug Type: SimpleXML related -Operating System: win xp sp2 +Operating System: * PHP Version: 5.2.5 New Comment: The comment is a node. What we actually need is a way to figure out the xml type of a SimpleXMLElement instance (Element, Comment,...). This will also have to return the internal SXE state (iterator for something or direct value). Previous Comments: [2007-12-10 20:29:17] 007not at gmail dot com hubert, you rigth about first test, i made mistake while i made posting of this bug. the right firts test looks such: //first test echo node:; var_dump($xml-node); echo notExitsNode:; var_dump($xml-notExitsNode); echo otherNode:; var_dump($xml-otherNode); Expected result: node: object(SimpleXMLElement)[2] public 'comment' = object(SimpleXMLElement)[4] notExitsNode: null otherNode: object(SimpleXMLElement)[2] Actual result: -- node: object(SimpleXMLElement)[2] public 'comment' = object(SimpleXMLElement)[4] notExitsNode: object(SimpleXMLElement)[2] otherNode: object(SimpleXMLElement)[2] Actually, that comment property might have been intentionally created as a way to indicate whether the node has a comment. But it is illogical, and we will have comments hell like: array(/*'comment' = 'value'*/) === array('comment' = 'value') As for your second test, I'm afraid it is incorrect may be (because i tryed to count nodes, and it must be 1 1), but when we use arrays we have problems with fake comments $i = 0; foreach ((array) $xml-node as $node) { $i++; } echo $i . \n; $i = 0; foreach ((array) $xml-otherNode as $node) { $i++; } echo $i . \n; Expected result: 0 0 Actual result: -- 1 0 # updated test: ?php $string = XML ?xml version='1.0'? document node!-- comment --/node otherNode/otherNode commentvalue/comment /document XML; $xml = simplexml_load_string($string); //first test echo node:; var_dump($xml-node); echo notExitsNode:; var_dump($xml-notExitsNode); echo otherNode:; var_dump($xml-otherNode); /* Expected result: node: object(SimpleXMLElement)[2] public 'comment' = object(SimpleXMLElement)[4] notExitsNode: null otherNode: object(SimpleXMLElement)[2] Actual result: -- node: object(SimpleXMLElement)[2] public 'comment' = object(SimpleXMLElement)[4] notExitsNode: object(SimpleXMLElement)[2] otherNode: object(SimpleXMLElement)[2] */ //second test $i = 0; foreach ($xml-node as $node) { $i++; } echo $i . \n; $i = 0; foreach ($xml-otherNode as $node) { $i++; } echo $i . \n; $i = 0; foreach ((array) $xml-node as $node) { $i++; } echo $i . \n; $i = 0; foreach ((array) $xml-otherNode as $node) { $i++; } echo $i . \n; /* Expected result: 1 1 0 0 Actual result: -- 1 1 1 0 */ //third test var_dump($xml-node-comment); var_dump($xml-otherNode-comment); //check magic echo node:\n; if (is_object($xml-node-comment)) { echo is_object === TRUE \n; } if (isset($xml-node-comment)) { echo isset === TRUE \n; } //but if (strlen($xml-node-comment) 0) { echo strlen 0\n; } if (strlen($xml-node-comment) == 0) { echo strlen == 0\n; } echo otherNode:\n; if (is_object($xml-otherNode-comment)) { echo is_object === TRUE \n; } if (isset($xml-otherNode-comment)) { echo isset === TRUE \n; } /* Expected result: node: is_object === TRUE isset === TRUE strlen == 0 otherNode: is_object === TRUE Actual result: -- node: is_object === TRUE strlen == 0 otherNode: is_object === TRUE */ [2007-12-09 14:39:46] hubert dot roksor at gmail dot com Regarding your first test, I wouldn't consider that a bug. var_dump() is a debugging tool, it may expose some of the behind-the-scene magic. Actually, that comment property might have been intentionally created as a way to indicate whether the node has a comment. That would explain isset()'s behaviour in your third test, but in this case I would recommand replacing that magical property with a method such as $node-hasComment(). I guess Rob Richards will be able to shed some light here. As for your second test, I'm afraid it is incorrect: both $xml-node and $xml-otherNode should return 1 element and I don't see why having a comment as a child would change that. [2007-12-09 11:02:10] 007not at gmail dot com Description: also see http://bugs.php.net/43392
#43542 [Opn]: simpleXML thinks that comment is node
ID: 43542 User updated by: 007not at gmail dot com Reported By: 007not at gmail dot com Status: Open Bug Type: SimpleXML related Operating System: win xp sp2 PHP Version: 5.2.5 New Comment: hubert, you rigth about first test, i made mistake while i made posting of this bug. the right firts test looks such: //first test echo node:; var_dump($xml-node); echo notExitsNode:; var_dump($xml-notExitsNode); echo otherNode:; var_dump($xml-otherNode); Expected result: node: object(SimpleXMLElement)[2] public 'comment' = object(SimpleXMLElement)[4] notExitsNode: null otherNode: object(SimpleXMLElement)[2] Actual result: -- node: object(SimpleXMLElement)[2] public 'comment' = object(SimpleXMLElement)[4] notExitsNode: object(SimpleXMLElement)[2] otherNode: object(SimpleXMLElement)[2] Actually, that comment property might have been intentionally created as a way to indicate whether the node has a comment. But it is illogical, and we will have comments hell like: array(/*'comment' = 'value'*/) === array('comment' = 'value') As for your second test, I'm afraid it is incorrect may be (because i tryed to count nodes, and it must be 1 1), but when we use arrays we have problems with fake comments $i = 0; foreach ((array) $xml-node as $node) { $i++; } echo $i . \n; $i = 0; foreach ((array) $xml-otherNode as $node) { $i++; } echo $i . \n; Expected result: 0 0 Actual result: -- 1 0 # updated test: ?php $string = XML ?xml version='1.0'? document node!-- comment --/node otherNode/otherNode commentvalue/comment /document XML; $xml = simplexml_load_string($string); //first test echo node:; var_dump($xml-node); echo notExitsNode:; var_dump($xml-notExitsNode); echo otherNode:; var_dump($xml-otherNode); /* Expected result: node: object(SimpleXMLElement)[2] public 'comment' = object(SimpleXMLElement)[4] notExitsNode: null otherNode: object(SimpleXMLElement)[2] Actual result: -- node: object(SimpleXMLElement)[2] public 'comment' = object(SimpleXMLElement)[4] notExitsNode: object(SimpleXMLElement)[2] otherNode: object(SimpleXMLElement)[2] */ //second test $i = 0; foreach ($xml-node as $node) { $i++; } echo $i . \n; $i = 0; foreach ($xml-otherNode as $node) { $i++; } echo $i . \n; $i = 0; foreach ((array) $xml-node as $node) { $i++; } echo $i . \n; $i = 0; foreach ((array) $xml-otherNode as $node) { $i++; } echo $i . \n; /* Expected result: 1 1 0 0 Actual result: -- 1 1 1 0 */ //third test var_dump($xml-node-comment); var_dump($xml-otherNode-comment); //check magic echo node:\n; if (is_object($xml-node-comment)) { echo is_object === TRUE \n; } if (isset($xml-node-comment)) { echo isset === TRUE \n; } //but if (strlen($xml-node-comment) 0) { echo strlen 0\n; } if (strlen($xml-node-comment) == 0) { echo strlen == 0\n; } echo otherNode:\n; if (is_object($xml-otherNode-comment)) { echo is_object === TRUE \n; } if (isset($xml-otherNode-comment)) { echo isset === TRUE \n; } /* Expected result: node: is_object === TRUE isset === TRUE strlen == 0 otherNode: is_object === TRUE Actual result: -- node: is_object === TRUE strlen == 0 otherNode: is_object === TRUE */ Previous Comments: [2007-12-09 14:39:46] hubert dot roksor at gmail dot com Regarding your first test, I wouldn't consider that a bug. var_dump() is a debugging tool, it may expose some of the behind-the-scene magic. Actually, that comment property might have been intentionally created as a way to indicate whether the node has a comment. That would explain isset()'s behaviour in your third test, but in this case I would recommand replacing that magical property with a method such as $node-hasComment(). I guess Rob Richards will be able to shed some light here. As for your second test, I'm afraid it is incorrect: both $xml-node and $xml-otherNode should return 1 element and I don't see why having a comment as a child would change that. [2007-12-09 11:02:10] 007not at gmail dot com Description: also see http://bugs.php.net/43392 [EMAIL PROTECTED] comment: This is just normal and expected behaviour. foo!-- comment --/foo is not same as foo/foo. (try var_dump($xml); to see what happens) i made some new test for you, and try to var_dump() this var_dump(array(/*'comment' = 'value'*/)); foo!-- comment --/foo === foo/foo array(/*'comment' = 'value'*/) === array('comment' = 'value') is it still be