php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71996 Using references in arrays doesn't work like expected
Submitted: 2016-04-08 20:38 UTC Modified: 2016-04-08 21:29 UTC
Votes:8
Avg. Score:4.6 ± 0.7
Reproduced:8 of 8 (100.0%)
Same Version:4 (50.0%)
Same OS:3 (37.5%)
From: frankdeweger at gmail dot com Assigned:
Status: Closed Package: SOAP related
PHP Version: 7.0.5 OS: CentOS
Private report: No CVE-ID: None
 [2016-04-08 20:38 UTC] frankdeweger at gmail dot com
Description:
------------
On of our suppliers expects heavily nested XML in the request we make.

Constructions like the following are not uncommon in the requests we have to make to them:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Body>
    <element1>
        <element2>
          <element3>
              <element4>
                <foo>bar</foo>
              </element4>
          </element3>
        </element2>
    </element1>
</SOAP-ENV:Body>

In order to create these nested structures, we used a helper as mentioned in the test script.

In PHP 5.4.45 it produced the XML as stated above. However, under PHP 7.0.5, the following XML is generated:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Body>
    <element1/>
</SOAP-ENV:Body>

For other people affected by this bug, if you replace the buildElement function with the following code, the generated XML is as expected.

    private function buildElement(array &$addTo, array $elements)
    {
        if (0 === count($elements)) {
            return;
        }
        $element = array_shift($elements);
        $addTo[$element] = array();
        $this->buildElement($addTo, $elements);
        return;
    }


Test script:
---------------
<?php
class Example
{
    private function buildElement(array &$addTo, array $elements)
    {
        $previous = & $addTo;
        foreach ($elements as $element) {
            $previous[$element] = array();
            $previous = & $previous[$element];
        }
    }

    public function getSoapData()
    {
        $soapData = array();
        $this->buildElement($soapData, array('element1', 'element2', 'element3', 'element4', 'foo'));
        $soapData['element1']['element2']['element3']['element4']['element4']['foo'] = 'bar';
        return $soapData;
    }
}

$client = new SoapClient("some.wsdl");
$data = (new Example())->getSoapData();
$client->doSomething($data);

Expected result:
----------------
The XML sent to our supplier looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Body>
    <element1>
        <element2>
          <element3>
              <element4>
                <foo>bar</foo>
              </element4>
          </element3>
        </element2>
    </element1>
</SOAP-ENV:Body>

Actual result:
--------------
The XML sent to our supplier looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Body>
    <element1/>
</SOAP-ENV:Body>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-04-08 21:29 UTC] frankdeweger at gmail dot com
I made small error in my test script, the correct code is:

    public function getSoapData()
    {
        $soapData = array();
        $this->buildElement($soapData, array('element1', 'element2', 'element3', 'element4', 'foo'));
        $soapData['element1']['element2']['element3']['element4']['foo'] = 'bar';
        return $soapData;
    }
 [2016-06-13 14:49 UTC] nikola at petkanski dot com
Probably has something with the fact PHP 7 completely revamped how zvars work.

We have the same problem. In order to reproduce the problem please use the following soap server and client combo. It clearly illustrates where the problem lies.

Soap Server:
https://gist.github.com/dinamic/29e7f7d6bd3085d903dfa1634c8cd65c

Soap Client:
https://gist.github.com/dinamic/e7e2d904c76553bda390e5d9b1a5bcca
 [2016-06-13 14:50 UTC] nikola at petkanski dot com
Forgot to mention the case is tested and works in PHP 5.6, but fails in PHP 7.0.
 [2016-06-16 12:57 UTC] peter at clearbooks dot co dot uk
Hello, I believe we're experiencing the same issue under PHP 7.0.7.

Here's a small test suite which reproduces the problem, resulting in a failing test. We simplified the test cases as much as we could:
https://github.com/clearbooks/soap-client-bug-test
 [2016-08-30 15:22 UTC] nikic@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=8e487aefaaf88bdad6343da06286bfc86063836c
Log: Fixed bug #71996
 [2016-08-30 15:22 UTC] nikic@php.net
-Status: Open +Status: Closed
 [2016-10-17 10:08 UTC] bwoebi@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=8e487aefaaf88bdad6343da06286bfc86063836c
Log: Fixed bug #71996
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 08:01:29 2024 UTC