Tutorial :Convert/cast an stdClass object to another class


I'm using a third party storage system that only returns me stdClass objects no matter what I feed in for some obscure reason. So I'm curious to know if there is a way to cast/convert an stdClass object into a full fledged object of a given type.

For instance something along the lines of:

//$stdClass is an stdClass instance  $converted = (BusinessClass) $stdClass;  

I am just casting the stdClass into an array and feed it to the BusinessClass constructor, but maybe there is a way to restore the initial class that I am not aware of.

Note: I am not interested in 'Change your storage system' type of answers since it is not the point of interest. Please consider it more an academic question on the language capacities.



See the manual on Type Juggling on possible casts.

The casts allowed are:

  • (int), (integer) - cast to integer
  • (bool), (boolean) - cast to boolean
  • (float), (double), (real) - cast to float
  • (string) - cast to string
  • (array) - cast to array
  • (object) - cast to object
  • (unset) - cast to NULL (PHP 5)

You would have to write a Mapper that does the casting from stdClass to another concrete class. Shouldn't be too hard to do.

Or, if you are in a hackish mood, you could adapt the following code:

function arrayToObject(array $array, $className) {      return unserialize(sprintf(          'O:%d:"%s"%s',          strlen($className),          $className,          strstr(serialize($array), ':')      ));  }  

which pseudocasts an array to an object of a certain class. This works by first serializing the array and then changing the serialized data so that it represents a certain class. The result is unserialized to an instance of this class then. But like I said, it's hackish, so expect side-effects.

For object to object, the code would be

function objectToObject($instance, $className) {      return unserialize(sprintf(          'O:%d:"%s"%s',          strlen($className),          $className,          strstr(strstr(serialize($instance), '"'), ':')      ));  }  


You can use above function for casting not similar class objects (PHP >= 5.3)

/**   * Class casting   *   * @param string|object $destination   * @param object $sourceObject   * @return object   */  function cast($destination, $sourceObject)  {      if (is_string($destination)) {          $destination = new $destination();      }      $sourceReflection = new ReflectionObject($sourceObject);      $destinationReflection = new ReflectionObject($destination);      $sourceProperties = $sourceReflection->getProperties();      foreach ($sourceProperties as $sourceProperty) {          $sourceProperty->setAccessible(true);          $name = $sourceProperty->getName();          $value = $sourceProperty->getValue($sourceObject);          if ($destinationReflection->hasProperty($name)) {              $propDest = $destinationReflection->getProperty($name);              $propDest->setAccessible(true);              $propDest->setValue($destination,$value);          } else {              $destination->$name = $value;          }      }      return $destination;  }  


class A   {    private $_x;     }    class B   {    public $_x;     }    $a = new A();  $b = new B();    $x = cast('A',$b);  $x = cast('B',$a);  


To move all existing properties of a stdClass to a new object of a specified class name:

/**   * recast stdClass object to an object with type   *   * @param string $className   * @param stdClass $object   * @throws InvalidArgumentException   * @return mixed new, typed object   */  function recast($className, stdClass &$object)  {      if (!class_exists($className))          throw new InvalidArgumentException(sprintf('Inexistant class %s.', $className));        $new = new $className();        foreach($object as $property => &$value)      {          $new->$property = &$value;          unset($object->$property);      }      unset($value);      $object = (unset) $object;      return $new;  }  


$array = array('h','n');    $obj=new stdClass;  $obj->action='auth';  $obj->params= &$array;  $obj->authKey=md5('i');    class RestQuery{      public $action;      public $params=array();      public $authKey='';  }    $restQuery = recast('RestQuery', $obj);    var_dump($restQuery, $obj);  


object(RestQuery)#2 (3) {    ["action"]=>    string(4) "auth"    ["params"]=>    &array(2) {      [0]=>      string(1) "h"      [1]=>      string(1) "n"    }    ["authKey"]=>    string(32) "865c0c0b4ab0e063e5caa3387c1a8741"  }  NULL  

This is limited because of the new operator as it is unknown which parameters it would need. For your case probably fitting.


I have a very similar problem. Simplified reflection solution worked just fine for me:

public static function cast($destination, \stdClass $source)  {      $sourceReflection = new \ReflectionObject($source);      $sourceProperties = $sourceReflection->getProperties();      foreach ($sourceProperties as $sourceProperty) {          $name = $sourceProperty->getName();          $destination->{$name} = $source->$name;      }      return $destination;  }  


Hope that somebody find this useful

// new instance of stdClass Object  $item = (object) array(      'id'     => 1,      'value'  => 'test object',  );    // cast the stdClass Object to another type by passing  // the value through constructor  $casted = new ModelFoo($item);    // OR..    // cast the stdObject using the method  $casted = new ModelFoo;  $casted->cast($item);  
class Castable  {      public function __construct($object = null)      {          $this->cast($object);      }        public function cast($object)      {          if (is_array($object) || is_object($object)) {              foreach ($object as $key => $value) {                  $this->$key = $value;              }          }      }  }   
class ModelFoo extends Castable  {      public $id;      public $value;  }  


Changed function for deep casting (using recursion)

/**   * Translates type   * @param $destination Object destination   * @param stdClass $source Source   */  private static function Cast(&$destination, stdClass $source)  {      $sourceReflection = new \ReflectionObject($source);      $sourceProperties = $sourceReflection->getProperties();      foreach ($sourceProperties as $sourceProperty) {          $name = $sourceProperty->getName();          if (gettype($destination->{$name}) == "object") {              self::Cast($destination->{$name}, $source->$name);          } else {              $destination->{$name} = $source->$name;          }      }  }  


BTW: Converting is highly important if you are serialized, mainly because the de-serialization breaks the type of objects and turns into stdclass, including DateTime objects.

I updated the example of @Jadrovski, now it allows objects and arrays.


$stdobj=new StdClass();  $stdobj->field=20;  $obj=new SomeClass();  fixCast($obj,$stdobj);  

example array

$stdobjArr=array(new StdClass(),new StdClass());  $obj=array();   $obj[0]=new SomeClass(); // at least the first object should indicates the right class.  fixCast($obj,$stdobj);  

code: (its recursive). However, i don't know if its recursive with arrays. May be its missing an extra is_array

public static function fixCast(&$destination,$source)  {      if (is_array($source)) {          $getClass=get_class($destination[0]);          $array=array();          foreach($source as $sourceItem) {              $obj = new $getClass();              fixCast($obj,$sourceItem);              $array[]=$obj;          }          $destination=$array;      } else {          $sourceReflection = new \ReflectionObject($source);          $sourceProperties = $sourceReflection->getProperties();          foreach ($sourceProperties as $sourceProperty) {              $name = $sourceProperty->getName();              if (is_object(@$destination->{$name})) {                  fixCast($destination->{$name}, $source->$name);              } else {                  $destination->{$name} = $source->$name;              }          }      }  }  


consider adding a new method to BusinessClass:

public static function fromStdClass(\stdClass $in): BusinessClass  {    $out                   = new self();    $reflection_object     = new \ReflectionObject($in);    $reflection_properties = $reflection_object->getProperties();    foreach ($reflection_properties as $reflection_property)    {      $name = $reflection_property->getName();      if (property_exists('BusinessClass', $name))      {        $out->{$name} = $in->$name;      }    }    return $out;  }  

then you can make a new BusinessClass from $stdClass:

$converted = BusinessClass::fromStdClass($stdClass);  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »