json_encode() custom object type

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
victorsk
Forum Commoner
Posts: 43
Joined: Thu Apr 19, 2007 6:55 pm

json_encode() custom object type

Post by victorsk »

Hi everyone,

Would anybody know how to use json_encode() to encode and preserve object type? What I have is the following:

Code: Select all

$client = new Web_Services_Object;
$client->CallAPI();
$obj = json_encode($client);
After json_decode($obj) is done, I get stdClass Object for:

Code: Select all


$myObj =  json_decode($obj); //is stdClass 

But I need $myObj to be of Web_Services_Object type. Does anybody know how object type Web_Services_Object can be preserved during json_encode() ???

Thank you,
Victor.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: json_encode() custom object type

Post by Eran »

JSON objects are basically associative arrays and don't have specific types. They are property containers, the same as stdClass, which is why it is decoded into such. You might want to create a method in your class to load back serialized JSON data
victorsk
Forum Commoner
Posts: 43
Joined: Thu Apr 19, 2007 6:55 pm

Re: json_encode() custom object type

Post by victorsk »

Hi,

Thanks for the message. Could you kindly post an example of what you mean? I am not much knowledgable with PHP :(

Thanks,
Victor.
Post Reply