Database Singleton

A singleton allows access to a single created instance - that instance (or rather, a reference to that instance) can be passed as a parameter to other methods, and treated as a normal object.

***************************************************************************

//Database singleton
include 'class/Database.php';
$db = Database::getInstance();
 

***************************************************************************
CLASS DABASE.PHP

<?php
/**
* Database Singleton
* @author david raleche <davidraleche@gmail.com>
*
*/

Class Database {
        private $_db;
        static $_instance;

        private function __construct(){
                $serveur = 'localhost';
                $login = 'YOURLOGIN';
                $pass = 'YOURPASSWORD';
          
                $bdd = 'YOURDATABSE_beta';
                $status = $this->_db = mysql_connect($serveur, $login, $pass);
            
                mysql_select_db($bdd, $this->_db);
        }

        private function __clone() {}

        public static function getInstance(){
                if(!(self::$_instance instanceof self)){
                        self::$_instance = new self();
                }
                return self::$_instance;
        }
}

?>
 

Latest Post

Popular Post

Our Skills

Author

Project Director Information Architect Database Administrator Unix Administrator Senior Developer Project Manager Product Manager Web Marketer Cloud Engineer

Contact Us

For any questions or project ideas please contact me at the following.

I'm here