|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2010-07-27 20:28 UTC] runpac314 at gmail dot com
[2010-07-28 11:22 UTC] ivan dot enderlin at hoa-project dot net
[2010-07-28 14:18 UTC] johannes@php.net
-Status: Open
+Status: Bogus
[2010-07-28 14:18 UTC] johannes@php.net
[2010-07-28 15:25 UTC] ivan dot enderlin at hoa-project dot net
[2010-07-28 17:36 UTC] ivan dot enderlin at hoa-project dot net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 06 01:00:01 2025 UTC |
Description: ------------ Hey :-), spl_object_hash() has strange behaviors with SimpleXML. You know that SimpleXMLElement uses properties to access to its children collection and array-access to reach a specific child into this collection. So, if we want to reach an element, we have to do: $sxe->element[0] for example. Unfortunately, it appears to always return a “new object” according to spl_object_hash(). Please, see the code below. You can notice that $sxe->p has sometimes the same has than $sxe-p[0]. And why $sxe-p[0] has most of the time a different hash? Test script: --------------- <?php $xml = '<?xml version="1.0" encoding="utf-8"?>' . "\n\n" . '<page>' . "\n" . ' <p>Foobar</p>' . "\n" . '</page>'; $sxe = simplexml_load_string($xml); function f ( $e ) { return spl_object_hash($e); } var_dump(f($sxe->p)); var_dump(f($sxe->p)); var_dump(f($sxe->p[0])); var_dump(f($sxe->p[0])); var_dump(f($sxe->p[0])); var_dump(f($sxe->p[0])); var_dump(f($sxe->p[0])); var_dump(f($sxe->p[0])); echo "\n" . 'Light!' . "\n\n"; function g ( $e ) { return substr(f($e), 14, 2); } var_dump(g($sxe->p)); var_dump(g($sxe->p)); var_dump(g($sxe->p[0])); var_dump(g($sxe->p[0])); var_dump(g($sxe->p[0])); var_dump(g($sxe->p[0])); var_dump(g($sxe->p[0])); var_dump(g($sxe->p[0])); Expected result: ---------------- string(32) "000000005cddd92f000000002401191e" string(32) "000000005cddd92f000000002401191e" string(32) "000000005cddd929000000002401191e" string(32) "000000005cddd929000000002401191e" string(32) "000000005cddd929000000002401191e" string(32) "000000005cddd929000000002401191e" string(32) "000000005cddd929000000002401191e" string(32) "000000005cddd929000000002401191e" Light! string(2) "2f" string(2) "2f" string(2) "29" string(2) "29" string(2) "29" string(2) "29" string(2) "29" string(2) "29" Actual result: -------------- string(32) "000000005cddd92f000000002401191e" string(32) "000000005cddd92f000000002401191e" string(32) "000000005cddd929000000002401191e" string(32) "000000005cddd92e000000002401191e" string(32) "000000005cddd92f000000002401191e" string(32) "000000005cddd929000000002401191e" string(32) "000000005cddd92e000000002401191e" string(32) "000000005cddd92f000000002401191e" Light! string(2) "2f" // p string(2) "2f" // p, same hash, oof string(2) "29" // p[0], ok string(2) "2e" // p[0], huh? string(2) "2f" // p[0], has the hash that p… why? string(2) "29" // p[0], like the first p[0] string(2) "2e" // p[0], we have a loop here string(2) "2f" // p[0], definitively, we have a loop.