Convert stdclass to object php

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.

Cheers

asked Jul 14, 2010 at 6:43

3

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], '"'], ':']
    ]];
}

answered Jul 14, 2010 at 6:54

GordonGordon

307k72 gold badges525 silver badges550 bronze badges

7

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;
}

EXAMPLE:

class A 
{
  private $_x;   
}

class B 
{
  public $_x;   
}

$a = new A[];
$b = new B[];

$x = cast['A',$b];
$x = cast['B',$a];

answered Mar 21, 2012 at 20:08

Adam PuzaAdam Puza

1,41413 silver badges9 bronze badges

10

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;
}

Usage:

$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];

Output:

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.

answered Jan 20, 2012 at 19:10

hakrehakre

187k48 gold badges418 silver badges802 bronze badges

3

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;
}

answered Aug 28, 2012 at 12:29

Sergei GSergei G

1,4393 gold badges18 silver badges26 bronze badges

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;
}

answered Dec 12, 2014 at 11:46

WizzardWizzard

1992 silver badges7 bronze badges

1

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;
        }
    }
}

Chris

2,0311 gold badge20 silver badges35 bronze badges

answered Jul 17, 2013 at 10:55

JadrovskiJadrovski

511 silver badge5 bronze badges

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];

answered Nov 12, 2018 at 1:07

pgee70pgee70

3,4064 gold badges33 silver badges39 bronze badges

And yet another approach using the decorator pattern and PHPs magic getter & setters:

// A simple StdClass object    
$stdclass = new StdClass[];
$stdclass->foo = 'bar';

// Decorator base class to inherit from
class Decorator {

    protected $object = NULL;

    public function __construct[$object]
    {
       $this->object = $object;  
    }

    public function __get[$property_name]
    {
        return $this->object->$property_name;   
    }

    public function __set[$property_name, $value]
    {
        $this->object->$property_name = $value;   
    }
}

class MyClass extends Decorator {}

$myclass = new MyClass[$stdclass]

// Use the decorated object in any type-hinted function/method
function test[MyClass $object] {
    echo $object->foo . '
'; $object->foo = 'baz'; echo $object->foo; } test[$myclass];

answered Sep 19, 2019 at 14:20

BenniBenni

1,02311 silver badges14 bronze badges

Yet another approach.

The following is now possible thanks to the recent PHP 7 version.

$theStdClass = [object] [
  'a' => 'Alpha',
  'b' => 'Bravo',
  'c' => 'Charlie',
  'd' => 'Delta',
];

$foo = new class[$theStdClass]  {
  public function __construct[$data] {
    if [!is_array[$data]] {
      $data = [array] $data;
    }

    foreach [$data as $prop => $value] {
      $this->{$prop} = $value;
    }
  }
  public function word4Letter[$letter] {
    return $this->{$letter};
  }
};

print $foo->word4Letter['a'] . PHP_EOL; // Alpha
print $foo->word4Letter['b'] . PHP_EOL; // Bravo
print $foo->word4Letter['c'] . PHP_EOL; // Charlie
print $foo->word4Letter['d'] . PHP_EOL; // Delta
print $foo->word4Letter['e'] . PHP_EOL; // PHP Notice:  Undefined property

In this example, $foo is being initialized as an anonymous class that takes one array or stdClass as only parameter for the constructor.

Eventually, we loop through the each items contained in the passed object and dynamically assign then to an object's property.

To make this approch event more generic, you can write an interface or a Trait that you will implement in any class where you want to be able to cast an stdClass.

answered May 30, 2019 at 14:51

asibyasiby

2,96426 silver badges31 bronze badges

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.

example

$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;
            }
        }
    }
}

answered Jan 16, 2018 at 18:05

magallanesmagallanes

6,2494 gold badges51 silver badges52 bronze badges

Convert it to an array, return the first element of that array, and set the return param to that class. Now you should get the autocomplete for that class as it will regconize it as that class instead of stdclass.

/**
 * @return Order
 */
    public function test[]{
    $db = new Database[];

    $order = array[];
    $result = $db->getConnection[]->query["select * from `order` where productId in [select id from product where name = 'RTX 2070']"];
    $data = $result->fetch_object["Order"]; //returns stdClass
    array_push[$order, $data];

    $db->close[];
    return $order[0];
}

answered May 29, 2020 at 9:13

CLUTCHERCLUTCHER

1,6961 gold badge15 silver badges28 bronze badges

Not the answer you're looking for? Browse other questions tagged php stdclass or ask your own question.

What is stdClass object in PHP?

The stdClass is the empty class in PHP which is used to cast other types to object. It is similar to Java or Python object. The stdClass is not the base class of the objects. If an object is converted to object, it is not modified.

How do I print a stdClass object?

If you just want to print you can use var_dump[] or print_r[] . var_dump[$obj]; print_r[$obj]; If you want an array of all properties and their values use get_object_vars[] .

How do you convert a std class object into an array?

OBJECT_K - result will be output as an associative array of row objects, using first columns values as keys [duplicates will be discarded]. ARRAY_A - result will be output as an numerically indexed array of associative arrays, using column names as keys.

How do you find the value of the stdClass object?

You create StdClass objects and access methods from them like so: $obj = new StdClass; $obj->foo = "bar"; echo $obj->foo; I recommend subclassing StdClass or creating your own generic class so you can provide your own methods. Thank you for your help!

Chủ Đề