Bug #74203 [Nab]: unset($element->{0}) on SimpleXMLElement not working

From: Date: Sun, 05 Mar 2017 10:08:38 +0000
Subject: Bug #74203 [Nab]: unset($element->{0}) on SimpleXMLElement not working
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-207697@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74203&edit=1 ID: 74203 Updated by: requinix@php.net Reported by: alepane at gmail dot com Summary: unset($element->{0}) on SimpleXMLElement not working Status: Not a bug Type: Bug Package: SimpleXML related Operating System: macOS and linux PHP Version: 7.0.16 Block user comment: N Private report: N New Comment: ->{0} wasn't supported to begin with; like I said it should have corresponded to a <0> element, but that's not valid XML. The fact that it did work was a side-effect of SimpleXMLElement's buggy implementation of the -> and [] operators, and in the 15 months since 7.0 it seems nobody else was making use of it. Do you feel strongly that it should be documented? Previous Comments: ------------------------------------------------------------------------ [2017-03-05 09:58:43] alepane at gmail dot com Ok, fair enough. But shouldn't this be documentated as a breaking change between PHP5 and PHP7? ------------------------------------------------------------------------ [2017-03-04 11:38:09] requinix@php.net I wouldn't have expected that to work in the first place - it looks like you're trying to remove a <0/>. On that note, if I remember correctly there were a few improvements to consistency in PHP 7's SimpleXMLElement, regarding how object (->) and array ([]) notation behaves, so that's probably why your example doesn't work anymore. Besides that, unset($seg_reg[0]) works in all versions, so I'm calling the other behavior undesirable and therefore this not a bug. https://3v4l.org/rtSBr ------------------------------------------------------------------------ [2017-03-04 11:22:47] alepane at gmail dot com Description: ------------ The instruction unset($seg_ref->{0}) works in PHP5.6 but not in PHP7 ... I haven't found this behaviour's change in any doc, so it's probably a bug. The attached example in PHP 5.6 returns the dom without <seg id="A1"/>, in PHP 7 returns also that element (so the unset is not working). Using $seg_ref[0] works in both version. Test script: --------------- <?php $data='<data> <seg id="A1"/> <seg id="A2"/> <seg id="A3"/> <seg id="A4"/> <seg id="A5"/> </data>'; $doc=new SimpleXMLElement($data); $seg_ref = $doc->seg; unset($seg_ref->{0}); echo $doc->asXml(); Expected result: ---------------- <data> <seg id="A2"/> <seg id="A3"/> <seg id="A4"/> <seg id="A5"/> </data> Actual result: -------------- <data> <seg id="A1"/> <seg id="A2"/> <seg id="A3"/> <seg id="A4"/> <seg id="A5"/> </data> ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=74203&edit=1

« previous php.bugs (#207697) next »