<?php

class dbConnforTutle
{

    //variable to hold connection object.
    protected static $db;

    //private construct - class cannot be instatiated externally.
    private function __construct()
    {

        try {
            // assign PDO object to db variable
            self::$db = new PDO('mysql:host=localhost;dbname=blradsolutions_tathagat', 'blradsolutions_tathagat', 'bO[2EXhmPsia');
            self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            self::$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            //Output error - would normally log this to error file rather than output to user.
            echo "Connection Error: " . $e->getMessage();
        }
    }

    // get connection function. Static method - accessible without instantiation
    public static function getConnection()
    {

        //Guarantees single instance, if no connection object exists then create one.
        if (!self::$db) {
            //new connection object.
            new dbConnforTutle();
        }

        //return connection.
        return self::$db;
    }
}