3

Here My class name is User, When I print my class properties I'm getting my objective name properly. After that, I encoded the data with json_encode(). and then I decoding with json_decode(). I'm getting stdClass objective why?

<?php
class User
{
    public $name;
    public $age;
    public $salary;
}
    $user = new User();
    $user->name = 'Siddhu';
    $user->age = 24;
    $user->salary = 7000.80;

print_r($user);
//Output: User Object ( [name] => Siddhu [age] => 24 [salary] => 7000.8 ) 

print_r(json_encode($user));
//Output: {"name":"Siddhu","age":24,"salary":7000.8}

$d = json_encode($user);


$s = json_decode($d);
print_r($s);
//Output: stdClass Object ( [name] => Siddhu [age] => 24 [salary] => 7000.8 ) 

If you noticed stdClass is coming, How can I change to user

Siddhartha esunuri
  • 1,094
  • 17
  • 28
  • 2
    because, json is text and not designed to preserve PHP classes, serialize will do that, and when you decode without true as the second argument, that's what you get. It's the expected behaviour. – ArtisticPhoenix Dec 19 '18 at 06:53
  • 3
    You may want to instead use `serialize($user)` and `unserialize($serialized_user)`. – B. Fleming Dec 19 '18 at 06:54
  • For example look at this [Sandbox](http://sandbox.onlinephpfunctions.com/code/12d8e50abc99fa4665a1ffa4a0f8f923cdc90469) In the output the json `{"bar":"bar"}` we lose any information contained about the class when converting it to JSON. – ArtisticPhoenix Dec 19 '18 at 07:04
  • Refer this article https://stackoverflow.com/questions/931407/what-is-stdclass-in-php – Dipti Dec 19 '18 at 07:20

2 Answers2

4

There is no direct way you can get data in class object either you need to used custom method to decode else you can use serialize() && unserialize() function to get data in the class object when you decode;

$serialized_user_object = serialize($user);

$deserialized_user_object = unserialize($serialized_user_object);

In json_decode you can pass true in second argument if you want data as array. like this.

var_dump(json_decode($json, true));

to know more about json_decode see here.

B. Fleming
  • 5,778
  • 1
  • 16
  • 29
joy
  • 1,857
  • 16
  • 33
3

The reason this happens can be demonstrated quite easily...

class foo{
    public $bar = 'bar';
}

$json = json_encode(new foo);

echo $json."\n\n";

print_r(json_decode($json));

Output

{"bar":"bar"}

stdClass Object
(
    [bar] => bar
)

Sandbox

As you can see the output {"bar":"bar"} contains no information about what if any class this was, it could have been ['bar'=>'bar'] and the JSON would be the same... Then when decoding it, you don't have json_decode set to return as an array (second argument set to true), which is fine as that's not really what you want, but when it's set that way you get stdClass objects instead of an associative array for items with non-numeric keys. In short there is no way to recover the class "foo" from the json {"bar":"bar"} as that information does't exist (you can make it exist, but that's another tale for another day).

Using serialize we get something very different.

class foo{
    public $bar = 'bar';
}

$json = serialize(new foo);

 echo $json."\n\n";

print_r(unserialize($json));

Output

O:3:"foo":1:{s:3:"bar";s:3:"bar";}

foo Object
(
    [bar] => bar
)

Sandbox

This O:3:foo means an Object with a class name 3 in length named "foo". So it preserves that information for PHP to use when "decoding" the data.

The whole thing reads like this:

Object (3) "foo" with 1 property named (s)tring (3) "bar" with a value of (s)tring (3) "bar", or something like that.

Make sense.

AS a note PHP's serialize is less portable then JSON, as it only works in PHP, It's also much harder to manually edit then JSON, but if you really want to encode a class than that is the easiest way to do it. That said you can also "repopulate" the class from JSON data, but that can also be hard to maintain.

ArtisticPhoenix
  • 20,683
  • 2
  • 18
  • 34