Hi all,
I would like to get my head around how I can create a singleton db pattern.
What I have now works though, but as I know its not a singleton - My idea is this:
- I would like a class that holds all config information and them implement that in different classes when needed --> Example here is that I am extending it, and using the config data in another class to connect.
Is this in general a good approach? Can I extend a class unlimited times to use the data in it? Or should I instead use another approach to deal with my configuration data and the way it is implemented, suggetions are welcome, as I would like to learn some better approaches..
// CONFIG DATA :
class credentials {
private static $config = array();
const DB_USER = 'root';
const DB_PASS = '';
const DB_HOST = 'localhost';
const DB_NAME = 'dev_dcs';
public function __construct() {
self::$config['user'] = self::DB_USER;
self::$config['pass'] = self::DB_PASS;
self::$config['host'] = self::DB_HOST;
self::$config['name'] = self::DB_NAME;
}
}
// DATABASE ACCESS :
class database extends credentials {
public function __construct()
{
try
{
if ( new MySQLi( parent::DB_HOST, parent::DB_USER, parent::DB_PASS, parent::DB_NAME ) == true )
{
echo 'DB CLASS WORKS.';
}
else
{
throw new Exception( "Database connection error" );
}
}
catch( Exception $e )
{
echo $e->getMessage();
}
}
}