0

During migration of my code from PHP5 to PHP7, I am facing some pretty issues, One of them are arrayObject(). arrayObject in PHP7 is not behaving as expected, what will be the alternate solution to achieve arrayObject functionality.

//I have some data in this carList array
$carList = array();  

$carArray = new ArrayObject();

//Go throuch each car record and populate the car object
foreach ($carList as $lst){
    $car = new carDetail();
    foreach ($lst as $key=>$value){
        $car->{lcfirst($key)} = $value;
    }

    $car = new SoapVar($car, SOAP_ENC_OBJECT, null, null, 'car');
    $carArray->append($car);
}
return $carArray;

It results in following Soap Envelope

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://logistics.website.eu">
    <SOAP-ENV:Body>
        <ns1:OutgoingNoosGoodsOrder>
            </carArray>
        </ns1:OutgoingNoosGoodsOrder>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Before migration, it is like this on PHP 5

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://logistics.website.eu">
    <SOAP-ENV:Body>
        <ns1:OutgoingNoosGoodsOrder>
            <carArray>
                 <car>
                   <make> Hyundai </make>
                   <model> Verna </model>
                   <year> 2019 </year>
                 </car>
                 <car>
                   <make> Audi </make>
                   <model> Q7 </model>
                   <year> 2022 </year>
                 </car>
            </carArray>
        </ns1:OutgoingNoosGoodsOrder>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
MoS
  • 11
  • 1
  • 2
    _arrayObject is not behaving as expected_ Then you should show us some code, explain what you expect and what is happening. – RiggsFolly Apr 11 '22 at 07:12
  • Thanks for your reply @RiggsFolly please check the updated question. – MoS Apr 12 '22 at 06:59
  • 1
    First thought! `$car = new SoapVar($car, SOAP_ENC_OBJECT, null, null, car)` the `car` in param 5 looks wrong? – RiggsFolly Apr 12 '22 at 07:20
  • Can you turn on PHP warning and notice? I suspect the problem is something else. – qrsngky Apr 12 '22 at 08:22
  • `$car = new SoapVar($car, SOAP_ENC_OBJECT, null, null, car)` the `car` in param 5 is to create a XML node car. The same code is working fine on PHP5, I don't know what's wrong with PHP7 – MoS Apr 12 '22 at 09:22
  • I suspect the problem is elsewhere, not in this part that you showed. Have you tried re-running the thing with `error_reporting(E_ALL);`? – qrsngky Apr 12 '22 at 09:32
  • 2
    `car` is an unquoted string (unless you have a constant with that name defined somewhere), which is deprecated in PHP 7.2.x; `'car'` will create a normal string. – qrsngky Apr 12 '22 at 09:33
  • @qrsngky Yes I tried, it is not giving me any error or warning and Yeah `car` is in string. I forgot to add here.. – MoS Apr 12 '22 at 10:21
  • Also, your first XML example currently has only closing ``, no opening ``. – qrsngky Apr 12 '22 at 10:28
  • Also, if the php part is fine, the problem may be somewhere within the `wsdl` file. – qrsngky Apr 12 '22 at 11:16
  • @qrsngky what to check in the wsdl file? – MoS Apr 15 '22 at 14:41
  • check any `type` that is related to `car` – qrsngky Apr 17 '22 at 17:13

1 Answers1

0

I tried running the following code in https://sandbox.onlinephpfunctions.com/ with PHP 7.4.28 and 5.6.40 :

<?php
class carDetail{}
const car = '3';
$carList = [
  "something" => [1,2,3],
    "another" => [4,5,6],
    "yet_another" => [7,8,9]
];

$carArray = new ArrayObject();

//Go throuch each car record and populate the car object
foreach ($carList as $lst){
    $car = new carDetail();
    foreach ($lst as $key=>$value){
        $car->{lcfirst($key)} = $value;
    }

    $car = new SoapVar($car, SOAP_ENC_OBJECT, null, null, car);
    $carArray->append($car);
}
var_dump($carArray);

?>

The results are exactly the same in PHP 7.4.28 and 5.6.40 (screenshot below): enter image description here

So the problem must be something else, and not how ArrayObject works.


If I were to guess, one thing that stopped working when migrating from PHP5 to PHP7 was:
using an empty string to initialize an array. Like

$myVar = ''; 
$myVar['key']=[1,2,3];

That doesn't work in PHP7, even though it does in PHP5.

qrsngky
  • 2,263
  • 2
  • 13
  • 10
  • Yeah you're right. When printing or dumping the final result array it's same. But when I am calling this API in SOAP UI it's showing me this extra tag. FYI, I am using Zend v1 and it's libraries – MoS Apr 12 '22 at 10:15
  • What is `this extra tag`? – qrsngky Apr 12 '22 at 10:26
  • I am referring to this question - [https://stackoverflow.com/questions/70891252/extra-tags-of-soap-envstruct-in-xml-by-using-soap-var-and-arrayobject-while-mov] – MoS Apr 12 '22 at 10:31
  • Is `"this API"` also based on php? – qrsngky Apr 12 '22 at 11:02
  • Yes, this API is based on PHP only – MoS Apr 12 '22 at 11:27