Now that we have covered enough of the basics, it is time that we finally get into coding the SessionManager
class.
1. Create a new file named
dbconnection.php
. Add the following code to that file:<?php class DBConnection { const HOST = 'localhost'; const PORT = 27017; const DBNAME = 'myblogsite'; private static $instance; public $connection; public $database; private function __construct() { $connectionString = sprintf('mongodb://%s:%d', DBConnection::HOST, DBConnection::PORT); try { $this->connection = new Mongo($connectionString); $this->database = $this->connection-> selectDB(DBConnection::DBNAME); } catch (MongoConnectionException $e) { throw $e; } } static public function instantiate() { if (!isset(self::$instance)) { $class = __CLASS__; self::$instance = new $class; } return self::$instance; } public function getCollection($name) { return $this->database->selectCollection($name); } }
2. Create another PHP script...