Computing Magazine

Convert StdClass Objects to SOAP Compliant Objects

Posted on the 29 October 2012 by Akahgy

Description:

Having an obtained StdClass Object, we require to change it into a SOAP structure object or Array compliant.

Solution:

Converting a StdClass Object with PhP basically means iterating the object and populating the public properties with elements from the provided object/array.

First we get the public properties using the PhP Reflection Class:

$reflect = new ReflectionClass($this);
$properties = $reflect->getProperties (ReflectionProperty::IS_PUBLIC);

Having these properties, we simply iterate them as follows:

foreach($properties as $property) {
$propertyName = $property->getName();
if (isset($data[$propertyName])) {
$this->{$propertyName} =  (is_object($data[$propertyName])) ?  (array)$data[$propertyName] : $data[$propertyName];
}
}

Additionally, we can start by converting the $data object into Array:

if (is_object($data)) {
$data = (array)$data;
}


Back to Featured Articles on Logo Paperblog

Magazine