Jump to content

An object in a second object when the second object is in the first object


NotionCommotion

Recommended Posts

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)

 

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.