0

So I have notice during a print_r statement this creates a circular reference.

Should I avoid having both classes having each other as a parameter?

Aside from the print_r statement (which php seems to recognize as circular and not continue to print_r forever) is there other reasons I should not do this?

Class Account
{
     private $id;
     private $name;
     private $User; /** User object **/

    function __construct($User, $name){ 
       $this->User = $User;
       $this->name = $name;  
    }


}
Class User
{
     private $id;
     private $name;
     private $accounts = array(); /** An array of Account Objects **/

     function __construct($name){
        $this->name = $name; 
     }
     function addAccount($Account){
        array_push($this->accounts, $Account);
     }

}

$User = new User("Joe");
$Account = new Account($User,"accountname");
$User->addAccount($Account);
Chris
  • 2,166
  • 1
  • 24
  • 37
  • Just wondering, why does `Account` need to know the `$User`? To me it seems redundant. – TMH Jan 06 '15 at 14:03
  • Google for Forward Referencing – Mawg says reinstate Monica Jan 06 '15 at 14:04
  • Well I did it because certain operations done on $Account use the $User object. The code above is simplified. I just started second guessing my application in this way. hence the question. – Chris Jan 06 '15 at 14:09
  • @Mawg I googled Forward Referencing as you suggested... I'm having a hard time interpreting it as an answer to my question. Some guidance maybe? – Chris Jan 06 '15 at 14:16

0 Answers0