NotionCommotion Posted August 30, 2018 Share Posted August 30, 2018 I was getting unexpected results with an application where objects are in one another and to troubleshoot, I tried echoing the objects out to JSON, but received different unexpected results. I created the following code to highlight the issue. I think I figured out the second unexpected part. I can't generate JSON because it will be recursive, true? Back to the first, I will continue to troubleshoot, but would like to know if there is any fundamental reason why one wouldn't want objects being in each other like this? Thanks <?php class Main implements \JsonSerializable { private $objA=[], $objB=[]; public function start() { for ($i = 0; $i < 10; $i++) { $objB=$this->getObjBandAddToStack($i); $this->objA[]=new ClassA($objB); } } private function getObjBandAddToStack($i) { if(isset($this->objB[$i/2])) { $objB=$this->objB[$i/2]; } else { $objB=new ClassB(); $this->objB[$i]=$objB; } return $objB; } public function jsonSerialize(){ return [ 'objA' => $this->objA, 'objB' => $this->objB, ]; } } class ClassA implements \JsonSerializable { private $objB; public function __construct(ClassB $objB) { $this->objB=$objB; $objB->joinGroup($this); } public function jsonSerialize(){ return [ 'objB' => $this->objB, ]; } } class ClassB implements \JsonSerializable { private $objAgroup=[]; public function joinGroup($objA) { $this->objAgroup[]=$objA; } public function jsonSerialize(){ return [ 'objAgroup' => $this->objAgroup, ]; } } $main=new Main(); $main->start(); $json=json_encode($main); //bool(false) Quote Link to comment Share on other sites More sharing options...
requinix Posted August 30, 2018 Share Posted August 30, 2018 You created recursion. It might make sense to have each object reference the other in normal usage, but for serialization it does not. Obviously you can't serialize both infinitely, which means it has to stop somewhere - logically, after the second object (whichever class it is). But now you've created a situation where the serialized form of both classes varies: one time it might have the nested object, one time it might not. That inconsistency is really annoying to deal with. Unfortunately this is one of those times when an abstract question doesn't have a definitive answer... Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted August 30, 2018 Author Share Posted August 30, 2018 Thanks requinix, I just wanted to make sure that PHP coudl handle it, and wouldn't somehow break how objects are passed by reference and instead make them different objects. The inability to serialize them is definitely understandable. Quote Link to comment Share on other sites More sharing options...
requinix Posted August 30, 2018 Share Posted August 30, 2018 Regular serialization (serialize/unserialize) can handle references and recursion. JSON serialization cannot. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.