PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Aug 16, 2018 7:53 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed Aug 23, 2017 5:21 pm 
Offline
Forum Contributor

Joined: Wed Sep 25, 2013 4:09 am
Posts: 172
Hi,

I have a class with some properties like id, name and stuff and some functions.
When i JSON encode it and send to my program I JSON decode it and get an object with the same properties as my class but without the functions of the class...
Is there a smart way of turning this JSON object to an object of my class or do i have to copy property for property?

As I do now:
Syntax: [ Download ] [ Hide ]
                    $product = new Models\Product;
                    $obj = json_decode($_POST["product"]);
           
                    $product->name = $obj->name;
                    $product->grossweight = $obj->grossweight;
                    $product->gw_unit = $obj->gw_unit;
                    $product->netweight = $obj->netweight;
                    $product->nw_unit = $obj->nw_unit;            
 


This works just fine but is there a better way to do it?

Thanks :)


Top
 Profile  
 
PostPosted: Wed Aug 23, 2017 6:01 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13572
Location: New York, NY, US
That is probably the most straightforward way to do it. I would create a importJson() method in Models\Product and put that code into the class.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Thu Aug 24, 2017 3:21 am 
Offline
Forum Contributor

Joined: Wed Sep 25, 2013 4:09 am
Posts: 172
Christopher wrote:
That is probably the most straightforward way to do it. I would create a importJson() method in Models\Product and put that code into the class.


I was so amazed when it just worked I almost fell off my chair.. I never used JSON so I thought I would spend weeks before I get it all to work and then 2 or 3 tries (5-10 mins or so) later it all worked haha.

Excellent suggestion to put it inside the class, will do that asap. Thanks :)


Top
 Profile  
 
PostPosted: Thu Aug 24, 2017 8:19 am 
Offline
Forum Contributor

Joined: Wed Sep 25, 2013 4:09 am
Posts: 172
Hmm it works great for single dimention arrays but for deeper arrays it convert it to stdClass Object

Like:
[ingredients] => Array ( [0] => Högpastöriserad mjölk [1] => mjölkprotein [2] ......)
which is OK (Single dimention)

[nutrition_info] => stdClass Object ( [fat] => stdClass Object ( [1.50] => g ) [fat saturated] => stdClass Object ( [1.10] => g ) [carbohydrate] => stdClass Object ( [4.60] => g ).....)
which is NOT OK..

Why does it suddenly treat next level as objects?

I typecasted it back to array like this:
Syntax: [ Download ] [ Hide ]
foreach ($obj->nutrition_info as $key => $value) {
            $this->nutrition_info[$key] = (array) $value;      //Typecasting std_object to array.
        }

which seems to work and produce the result I want but I'm not sure if it is the correct way of doing it and it feels so so since I don't know why the arrays turned into objects in the first place?

Here is the result after the typecast:
[nutrition_info] => Array ( [fat] => Array ( [1.50] => g ) [fat saturated] => Array ( [1.10] => g ) [carbohydrate] => Array ( [4.60] => g ) ......)

Do I need to worry?


Top
 Profile  
 
PostPosted: Thu Aug 24, 2017 9:39 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13572
Location: New York, NY, US
The reason that the arrays are defined as objects is due to json_decode(). That is the standard behavior. There is a second parameter with some flags that may help control this. However, what you are doing is fine, and converts it to an array which is what you want.

FYI, objects and arrays are similar in PHP, so you could also experiment with doing something like this:
Syntax: [ Download ] [ Hide ]
$merged_obj = (object) array_merge((array) $product, (array) $obj);

_________________
(#10850)


Top
 Profile  
 
PostPosted: Sat Aug 26, 2017 6:46 am 
Offline
Forum Contributor

Joined: Wed Sep 25, 2013 4:09 am
Posts: 172
Aha ok thanks :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: ckblackmoncler, Majestic-12 [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group