php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #75137 Insufficient documentation regarding namespaces
Submitted: 2017-08-30 13:46 UTC Modified: 2017-09-13 15:24 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: marijn at suninet dot org Assigned:
Status: Verified Package: SimpleXML related
PHP Version: 7.0.22 OS: Linux
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2017-08-30 13:46 UTC] marijn at suninet dot org
Description:
------------
Hi,

I've been looking around but I haven't been able to find anything. If you assign a value to an XML element by using an array key when XML namespaces are used the behaviour is changed between PHP 5.6 and PHP 7.0. This is something we couldn't anywhere in the documentation around backwards incompatibility. It could have something to do with "Changes to the handling of indirect variables, properties, and methods" but from my understanding reading that section that is about when you use dynamic variable assignments which is not the case here.

In the expected/actual result you can see the difference. The expected result is what you get when running the sample code in PHP 5.6 and the actual result is what you get when running the sample code in PHP 7.0.

My guess is that this is not a bug but rather an undocumented backward incompatible change between the PHP versions.

Regards,

Marijn.

Test script:
---------------
<?php

$xml = new SimpleXMLElement('<xml xmlns="http://xml" xmlns:foo="http://foo" xmlns:bar="http://bar" />');

$foo = $xml->addChild('foo', null, 'http://foo');

$tag = $foo->addChild('bar', null, 'http://bar');
$foo->bar[0] = 'Hello World!';

$dom = dom_import_simplexml($xml)->ownerDocument;
$dom->formatOutput = true;

echo $dom->saveXML();

Expected result:
----------------
$ php-5.6 test.php
<?xml version="1.0"?>
<xml xmlns="http://xml" xmlns:foo="http://foo" xmlns:bar="http://bar">
  <foo:foo>
    <bar:bar>Hello World!</bar:bar>
  </foo:foo>
</xml>

Actual result:
--------------
$ php-7.0 test.php
<?xml version="1.0"?>
<xml xmlns="http://xml" xmlns:foo="http://foo" xmlns:bar="http://bar">
  <foo:foo>
    <bar:bar/>
    <foo:bar>Hello World!</foo:bar>
  </foo:foo>
</xml>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-08-31 14:31 UTC] cmb@php.net
According to <https://3v4l.org/W5n3d> the behavioral change has
been introduced in PHP 7.0.12.
 [2017-08-31 17:42 UTC] marijn at suninet dot org
-Summary: Undocumented break from 5.6 using XML namespaces and assigning value by key. +Summary: Breaking change in 7.0.12 using XML namespaces and assigning value by key.
 [2017-08-31 17:42 UTC] marijn at suninet dot org
I guess it may be a bug then after all.
 [2017-09-01 16:34 UTC] cmb@php.net
-Status: Open +Status: Verified -Type: Documentation Problem +Type: Bug
 [2017-09-01 16:34 UTC] cmb@php.net
A git bisect points at
<http://git.php.net/?p=php-src.git;a=commit;h=6adb7e0>, and the
commit message indeed states that the fix is incomplete. So yes,
this is a bug.
 [2017-09-01 16:59 UTC] nikic@php.net
Based on vague memory, the "incomplete" part here is that it inherits the foo namespace rather than inserting it without namespace. I believe the fully correct output in this case would be:

<?xml version="1.0"?>
<xml xmlns="http://xml" xmlns:foo="http://foo" xmlns:bar="http://bar">
  <foo:foo>
    <bar:bar/>
    <bar>Hello World!</bar>
  </foo:foo>
</xml>

The correct way to write this code is:

$foo->children('http://bar')->bar[0] = 'Hello World!';

SimpleXML requires you to explicitly specify the namespace. Prior to the referenced bug/commits this was somewhat hit-and-miss, with some places using the namespace and some places changing *all* elements with a given name regardless of namespace.
 [2017-09-11 04:55 UTC] marijn at suninet dot org
-Summary: Breaking change in 7.0.12 using XML namespaces and assigning value by key. +Summary: marijn@suninet.org
 [2017-09-11 04:55 UTC] marijn at suninet dot org
That sounds reasonable, however it would be great if information like that could be added to the documentation. The SimpleXMLElement documentation is so sparse, especially regarding namespace usage, that in my opinion it's impossible to know, not even through inference, that code like the example should be written like you suggested. That is also probably how this code was birthed. A developer had to figure out how to do this and due to lack of documentation came to this working solution through trial and error. However then in a patch version and without mentioning it in the release notes this behaviour was broken/changed.
 [2017-09-13 15:24 UTC] cmb@php.net
-Summary: marijn@suninet.org +Summary: Insufficient documentation regarding namespaces -Type: Bug +Type: Documentation Problem
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 29 15:01:52 2017 UTC