0

I want to make sure I don't start multiple database connections when creating objects. Being new to OOP I'd like to get this right the first time.

I am using MongoDB

//connect to mongodb
$dbhost = 'localhost';
$dbname = 'test';
$mongo = new Mongo("mongodb://$dbhost");

So this is creating the database object. I read that it is best to use dependency injection into the class like below.

class Database {
    protected $dbc; //database connection
    protected $db; //database

    function __construct(Mongo $mongo, $dbname) {
        $this->dbc = $mongo;
        $this->db = $this->dbc->$dbname;
    }
}

The object is created by using this:

$db = new Database($mongo, $dbname);

And I'm able to use the MongoDB connection with no problems inside the Database class.

Since I'm creating a REST server, I'd like to split my Database class up into each method (PUT, GET, DELETE, etc). So really, I think it'd be best to extend the Database class and then create an object for only the method the server is requesting.

class GetDatabase extends Database {
    //do my GET stuff in here
    //use the Mongo connection in the parent contruct
}

I will need to run more than just queries from the GetDatabase class, since I have an Auth class (does not extend the database) that needs to use the DB connection as well.

Can anyone provide advice on how to make this work better or ensure that only one database connection is created ever?

Thanks!

frustratedtech
  • 423
  • 4
  • 9
  • 1
    If you're always using DI to inject the `Mongo` instance into the classes that require it, there shouldn't be anything else creating connections – Phil Dec 11 '13 at 02:54
  • @Phil That's my thought as well, but when I go to create a new GetDatabase Object after I've created a PutDatabase object, will this use the same MongoDB connection or will it somehow create two of them? – frustratedtech Dec 11 '13 at 02:56
  • 1
    Why would any more be created? The only time creation happens is when you use `new Mongo()` – Phil Dec 11 '13 at 02:59
  • @Phil Ok. So once the object is created then it will stay an object even if you pass the object into a class? This is what I would assume as well, but since you call `new Database($mongo, $dbname)` I wasn't sure if it also created another MongoDB connection as well. Is this correct? – frustratedtech Dec 11 '13 at 03:09
  • 1
    The same object is passed around. As I said, the only time creation (and connection) happens is when you call `new Mongo()` – Phil Dec 11 '13 at 03:37
  • I would just do a variation on [this approach](http://stackoverflow.com/a/11369679/727208) .. only replacing PDO-related code with one for Mongo. – tereško Dec 11 '13 at 08:38

1 Answers1

-1

Instead of using the code

//connect to mongodb 
$dbhost = 'localhost'; 
$dbname = 'test'; 
$mongo = new Mongo("mongodb://$dbhost");

I will suggest you to use the following code.

class CMongo {
    private static $mongo_conn = null;

    public static function connection(){
        if(!self::$mongo_conn){
            $dbhost = 'localhost';
            $dbname = 'test';
            try{
                self::$mongo_conn = new Mongo("mongodb://$dbhost");
            }
            catch(Exception $e){
                $error_msg = $e->getMessage();
            }
        }
        return self::$mongo_conn;
    }
}

$mongo = CMongo::connection();

The above code will ensure that new Mongo("mongodb://$dbhost"); is called only once. Again the try {} catch {} block will ensure that the program do not terminate with fatal error when mongo is down or unreachable.

techstunts
  • 92
  • 5