简体   繁体   中英

PHP: Return Array of Custom Objects with NuSoap WebService

I have created the following custom class in PHP:

<?php

class myClass
{
   public $property1;
   public $property2;
}
?>

I have a NuSoap Webservice that I want to use to return an array of these objects in XML format. I have built the following function to return the data:

foreach($response->return->object as $object)
        {
            $returnObject = new $myClass;
            $returnObject->property1 = $object->property1;
            $returnObject->property2 = $object->property2;
            array_push($returnObjects, $returnObject);
        }
    }
    $result = array_unique($returnObjects);
    if (count($result) != 0){
    return $result;}

When I run the method, I get the following error:

Object of class MyClass could not be converted to string

Any assistance would be greatly appreciated! Thanks in advance.

Object creation is wrong.

$returnObject = new $myClass;

change above line to following

$returnObject = new myClass();

This post ended up being my solution:

Notice Array to string conversion using nusoap

Evidently there is a debugging conversion that breaks when you use complex data types. Luckily line 6132 can be commented out in the NuSoap.php without causing any issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM