title: Binary Associations between PHP Classes
body: <p>Hi everyone I have two classes a User and a Shopping cart which model a binary association between them. In Order to implement it i have created two classes</p>
  
  <p>A User class:</p>
  
  <pre><code>class User{
      protected $name ;
      protected $email;
      protected $shoppingCart;
  
      public function __construct($name , $email)
      {
          $this-&gt;name =$name;
          $this -&gt;email =$email;
          $this-&gt;shoppingCart=new ShoppingCart($this);
      }
  
      //other getter and setter functions
  }
  
  class ShoppingCart{
      //Array stores the list of products in the cart:
      protected  $products = array();
  
      // For storing the IDs, as a convenience:
      protected $ids = array();
  
      // protected user    
      protected $user;
  
      // Constructor just sets the object up for usage:    
      function __construct($user) {
          $this-&gt;products = array();
          $this-&gt;ids = array();
          $this-&gt;user =$user;
      }
  
      // functions to add or delete item    
  }
  </code></pre>
  
  <p>My Question is , is it ok for both user class and Shopping cart class to have an instance of each other so when I construct user , i construct its shopping cart in its constructor. and when i construct the shopping cart , I pass the user infor in the shopping carts constructor to assign the shopping cart to the user.</p>
  
  <p>How can i model this association properly. Also i would have to access the users shopping cart to display its total price from main PHP script...in that case can i also make the ShoppingCart attribute of Users Class public</p>