Help with connecting to a database in a class

Hey there. I am trying to connect to a DB in a class so I have created this class that opens a new persistent connection if one is not opened already.

private static function connect(){
                self::$connection  = new PDO($this->type.':host='.$this->host.';dbname='.$this->name, $this->user, $this->password);

            catch(\\PDOException $e){
            return self::$connection;

but when I try to connect to the database I end up getting a “Access to undeclared static property: Fusion\Core\db::$connection” error.

Can someone point me in the right direction.


Would… you not be wanting to point to $this instead of self:: ?

My understanding (and i’m not an OOP specialist, sadly) is that self:: refers to the class definition, whereas $this refers to the instantiated object of the class…

The PDO documentation seems to indicate you should be able to establish persistance with a setting in the connection attempt…

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
class Database {

    private $_connection;
    // Store the single instance.
    private static $_instance;

    // Get an instance of the Database.
    // @return Database:
    public static function getInstance() {
        if (!self::$_instance) {
            self::$_instance = new self();
        return self::$_instance;

    // Constructor - Build the PDO Connection:
    public function __construct() {
        $db_options = array(
            PDO::ATTR_EMULATE_PREPARES => false                     // important! use actual prepared statements (default: emulate prepared statements)
            , PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION           // throw exceptions on errors (default: stay silent)
            , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC      // fetch associative arrays (default: mixed arrays)
        $this->_connection = new PDO('mysql:host=localhost;dbname=cms;charset=utf8', 'username', 'password', $db_options);

    // Empty clone magic method to prevent duplication:
    private function __clone() {


    // Get the PDO connection:
    public function getConnection() {
        return $this->_connection;


To connect to the database/table :

$db = Database::getInstance();
$pdo = $db->getConnection();