title: How to connect to MongoDB from another PHP class?
body: <p>I have the following code to connect to MongoDB:</p>
  
  <pre><code>try {
     $m = new Mongo('mongodb://'.$MONGO['servers'][$i]['mongo_host'].':'.$MONGO['servers'][$i]['mongo_port']);
  
   } catch (MongoConnectionException $e) {
     die('Failed to connect to MongoDB '.$e-&gt;getMessage());
   }
  
  // select a database
  $db = $m-&gt;selectDB($MONGO["servers"][$i]["mongo_db"]);
  </code></pre>
  
  <p>Then I've created a PHP class where I want to retrieve/update data in Mongo. I don't know how to access the connection to Mongo, previously created.</p>
  
  <pre><code>class Shop {
  var $id;
  
  public function __construct($id) {
      $this-&gt;id = $id;        
      $this-&gt;info = $this-&gt;returnShopInfo($id);
      $this-&gt;is_live = $this-&gt;info['is_live'];
  }
  //returns shop information from the database
  public function returnShopInfo () {
      $where = array('_id' =&gt; $this-&gt;id);
      return $db-&gt;shops-&gt;findOne($where);
  }
  }
  </code></pre>
  
  <p>And the code is something like:</p>
  
  <pre><code>$shop = new Shop($id);
  print_r ($shop-&gt;info());
  </code></pre>