How can I implement a database class for all other classes? - php

I have a large amount of classes for: a main forums class, a forum boards class, and a threads class. These can be thought of as models in the context of my website.
The setup the previous web developer was using creates a new mysqli connection in each one of these classes and connects on instantiation of the class. This means that in the threads page there are 3 different connections for forums, threads, and users. This is obviously not ideal and I think not even optimal.
I worked on a previous project in which I could simply just pass a new instance of the database class to the class I was using, but this isn't ideal because I need to instantiate multiple classes and passing a database instance to each class would defeat the purpose.
In each of these classes, database calls are made so the database object/instance is needed within each class, without having to instantiate it 3+ separate times.
For example, in the threads class you could have:
function get_threads($board_id) {
return $this->con->query("some query");
}
Does anyone know how I can go about achieving this?

I prefer one database controller that you could get data from as in a factory pattern. In the example below, you could just create one controller and let your components use it as they need.
ex: $threads = new ThreadClass($mysqlConnection);
I hope this helps
# 1. mysql source or "socket" using PDO
class SimpleDatabaseSocket {
protected static $dbh = false;
public function connect($database, $user, $pass, $host) {
$dsn = "mysql:dbname={$database};host={$host}";
self::$dbh = new PDO($dsn, $user, $pass);
self::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
}
# 2. always use an interface which defines required logic
interface SimpleDatabaseInterface {
public function onRunQuery($query);
}
# 3. create the accessible controller for inherited & ancillary logic
class SimpleDatabaseController extends SimpleDatabaseSocket implements SimpleDatabaseInterface {
private $host = "";
private $base = "";
private $usr = "";
private $pwd = "";
private $table = "";
# when we create the controller, you MUST pass the connection params
public function __construct($host, $usr, $pwd, $base) {
$this->setUsr($usr);
$this->setPwd($pwd);
$this->setBase($base);
$this->setHost($host);
}
# mysql host/ip
public function setHost($host) {
$this->host = $host;
}
# mysql database
public function setBase($base) {
$this->base = $base;
}
# mysql username
public function setUsr($usr) {
$this->usr = $usr;
}
# mysql password
public function setPwd($pwd) {
$this->pwd = $pwd;
}
# allow this controller to switch tables dynamically.
public function onChangeTable($table) {
$this->table = $table;
}
# connect to the database
protected function onConnect() {
$this->connect($this->base, $this->usr, $this->pwd, $this->host);
}
# connects to the DB and runs a custom query
public function onRunQuery($query) {
try {
if (!self::$dbh)
$this->onConnect();
$result = self::$dbh->query($query);
$out = $result->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
}
return $out;
}
}
# exmaple usage
$mysql = new SimpleDatabaseController("HOSTNAME","USERNAME","PASSWORD","DATABASE");
$result = $mysql->onRunQuery("SELECT * FROM TABLE LIMIT 0,2");
print("<pre>" . print_r($result, true) . "</pre>");

Related

Using a Persistent Database Object Connection from Other Objects in PHP

This is in reference to a CentOS 7 server running PHP 5.4 and MariaDB 5.5.
I am somewhat new to OOP in PHP. In converting a bunch of scripts from MySQL to MySQLi and from procedural database functions to OOP, I set up this basic Database class:
class Database
{
private $host = "localhost";
private $username;
private $password;
private $database;
private $dbconnect;
function __construct()
{
// Load config file for database connection info
$ini = parse_ini_file("config.ini");
$this->username = $ini['db.user'];
$this->password = $ini['db.pword'];
$this->database = $ini['db'];
}
public function connect()
{
// Only make a new connection if one not already established.
if (empty($this->dbconnect)) {
$mysql = new mysqli($this->host, $this->username, $this->password, $this->database);
if ($mysql->connect_errno) {
throw new appError($mysql->connect_error);
}
$this->dbconnect = $mysql;
}
return $this->dbconnect;
}
public function query($query)
{
$db = $this->connect();
$result = $db->query($query);
if ($db->errno) return false;
return $result;
}
public function select($query)
{
$rows = array();
$result = $this->query($query);
if ($result === false) return false;
// Create array with results
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
}
With the former procedural database functions that used mysql_*, a persistent database connection was made near the start of the script and the resource ID was stored in a global variable, then all queries were run through that one database connection by accessing the global resource variable. Importantly, this worked well from within other functions and objects. Here's how that function worked:
function db_connect() {
if (!empty($GLOBALS['DBCONNECT']) && is_resource($GLOBALS['DBCONNECT'])) {
return $GLOBALS['DBCONNECT'];
} else {
$result = mysql_connect("localhost", DB_USER, DB_PASSWORD);
$GLOBALS['DBCONNECT'] = $result;
return $result;
}
}
So at the start of each script I'd do this...
db_connect();
And then run my queries like this...
$result = mysql_query($query, db_connect());
This made sure one database connection was made and that all queries are run through that connection.
With the above new Database class, I instantiate it at the start of my script...
$db = new Database;
$db->connect();
But I don't understand how to make that Database object accessible to other objects that need to perform database queries so that the same database connection is used by the entire script. What I do now is essentially this...
class MyClass
{
public function myFunction()
{
$db = new Database;
$data = $db->select("SELECT * FROM mydata WHERE id = 888");
...
}
}
This instantiates a new Database object within the above class, which is creating a new and additional connection to the database, because it can't access the Database $db object created in the parent calling script (that I know of).
Is there a way to use an object to open a persistent MySLQi database connection that all functions and objects loaded by that script can use? Or is this just something that is better done with a procedural function rather than a class and object? Does the solution lie in making the Database properties and its methods static? If I do that, I'm not sure how I load the database username and password from the config.ini file which the connect() function needs and is only done upon instantiation of the Database object.
I guess the basic question here is how do I access the properties or methods in an instantiated object from another object? Or maybe that isn't the question and I'm completely missing something else. Thanks!
You can do something like that a static method with a only a single tone
<?php
/*
* Mysql database class - only one connection alowed
*/
class Database {
private $_connection;
private static $_instance; //The single instance
private $_host = "HOSTt";
private $_username = "USERNAME";
private $_password = "PASSWORd";
private $_database = "DATABASE";
/*
Get an instance of the Database
#return Instance
*/
public static function getInstance() {
if(!self::$_instance) { // If no instance then make one
self::$_instance = new self();
}
return self::$_instance;
}
// Constructor
private function __construct() {
$this->_connection = new mysqli($this->_host, $this->_username,
$this->_password, $this->_database);
// Error handling
if(mysqli_connect_error()) {
trigger_error("Failed to conencto to MySQL: " . mysql_connect_error(),
E_USER_ERROR);
}
}
// Magic method clone is empty to prevent duplication of connection
private function __clone() { }
// Get mysqli connection
public function getConnection() {
return $this->_connection;
}
}
and to make a connection to the database and make a query simple use the lines:
$db = Database::getInstance();
$mysqli = $db->getConnection();
$sql_query = "SELECT foo FROM .....";
$result = $mysqli->query($sql_query);
There are a few options, one simple one (and likely the preferred) would be to use a static factory method in your database class that returns a previously initialized instance of the database class.
class Database
{
//static instance of the class
private static $instance;
public static function getDB()
{
//if the instance is not set, set it
if(is_null(self::$instance))
{
self::$instance = new Database();
self::$instance->connect();
}
return self::$instance;
}
//rest of your class below
}
This static method can be called from anywhere in any context and would allow for getting the same connected instance of the database. Just call $db = Database::getDB() anywhere you need it. For example:
function TestFunction()
{
//get an instance to the database
$dbInstance = Database::getDB();
//then use the database object like you normally would.
$dbInstance->query("SELECT * FROM `someTable`");
}
class TestClass
{
public function doSomething()
{
//get an instance to the database
$dbInstance = Database::getDB();
//then use the database object like you normally would.
$dbInstance->query("SELECT * FROM `someTable`");
}
}
$tc = new TestClass();
$tc->doSomething();
Another option, and one that I use for simplicity, is declare your connection static and reference that everywhere in your database class for queries and such. This allows for creating $db = new Database() anywhere and will just use the same connection. I've had this frowned upon as if it were same as a global because you can really only connect to one database (subsequent connections would overwrite the connection variable) But it worked for me and I didn't need multiple connections. I also like it that I can have each instance remember whatever queries were run on that instance and have a private count of the queries run.

PHP extending MySQL master class TWICE

Below is the solution to my question. I didn't have my class written wrong. I accidentally used a "die" function inside my master DB class that made all my queries fail. I was tracking down the wrong problem.
I hope you guys find this class example useful. It's clean, and very useful for wrapping SQL calls in a single location for multiple databases. By allowing the extension, you can track your calls much easier my var name.
class A extends DB {
protected $connection;
function __construct()
{
$this->db_host = "server.com:3327";
$this->db_name = "db_name_one";
$this->db_username = "root";
$this->db_password = 'pw';
// .. equals all the setup vars
parent::__construct();
}
}
class B extends DB {
function __construct()
{
$this->db_host = "server.com:3327";
$this->db_name = "db_name_two";
$this->db_username = "root";
$this->db_password = 'pw';
// .. equals all the setup vars
parent::__construct();
}
}
class DB {
function __construct()
{
$this->connect()
}
public function connect()
if (!$connection = # mysql_connect ($this->db_host,$this->db_username,$this->db_password,$this->second_flag))
die ('I cannot connect to the database because: ' . mysql_error());
if (!mysql_selectdb($this->db_name,$connection))
$this->showerror();
return $connection;
}
public function executeSQL($query)
{
$results = mysql_query($query,$this->connection);
if (!$results) {
die(...);
}
return $results;
}
}
$db1 = new A();
$db2 = new B();
$db1->executeSQL("select * from table");
I don't know if there's a legitimate reason to extend the DB class here. Unless those extending classes add something to the DB (e.g. they adapt it to a different database), then they have nothing to do with being a DB as such. Just because they use a DB doesn't mean they should extend one.
The simplest thing to do is: make one class that handles your database interaction. When you make a new instance of that class, it establishes a new connection to the database. This way you can have as many independent connections as you want. You then simply pass those instances to whoever needs them:
class DB {
protected $connection;
public function __construct() {
$this->connection = mysql_connect(..);
if (!$this->connection) {
throw new Exception(..);
}
}
..
}
class A {
protected $db;
public function __construct(DB $db) {
$this->db = $db;
}
..
}
$db = new DB;
$a = new A($db);

PHP OOP database connect

I have just started learning the concept of Object oriented programming and I've written this class with functions.
It works fine, but I'm interested in knowing if I have done this correctly...
Here is my code:
class Database{
const DB_HOSTNAME = 'localhost';
const DB_USERNAME = 'root';
const DB_PASSWORD = 'password';
const DB_NAME = 'shop';
protected $_db_connect;
protected $_sql;
protected $_result;
protected $_row;
function db_connect(){
$this->_db_connect = mysql_connect(self::DB_HOSTNAME,self::DB_USERNAME,self::DB_PASSWORD) or die(mysql_error());
}
function slect_db(){
mysql_select_db(self::DB_NAME) or die(mysql_error());
}
function sql(){
$this->_sql = 'SELECT * FROM users';
}
function query(){
$this->_result = mysql_query($this->_sql);
}
function fetch_array(){
while($this->_row = mysql_fetch_array($this->_result)){
$username = $this->_row['user_USERNAME'];
echo "<ul>";
echo "<li>".$username."</li>";
echo "</ul>";
}
}
function db_close(){
mysql_close($this->_db_connect);
}
}
$database = new Database();
$database->db_connect();
$database->slect_db();
$database->sql();
$database->query();
$database->fetch_array();
$database->db_close();
Please use PDO or MySQLi as it's more secure and mysql_* functions are deprecated as stated above, I've provided some generic code using PDO to help you with that new venture. As stated in the comments, you really shouldn't be echoing out the data if you're looking for an object oriented design, what you should be doing is after you do the query, return the rows that you fetched and from there use something such as a foreach loop to then display your data. Doing your database class this way will also make sure that multiple database connections aren't open at once! Note, this code is just for you to reference, and should be tested before use in production or anything live.
config.php:
<?php
//Enter your database connection details here.
$host = 'localhost'; //HOST NAME.
$db_name = 'databasename'; //Database Name
$db_username = 'root'; //Database Username
$db_password = ''; //Database Password
try
{
$pdo = new PDO('mysql:host='. $host .';dbname='.$db_name, $db_username, $db_password);
}
catch (PDOException $e)
{
exit('Error Connecting To DataBase');
}
?>
database.class.php:
<?php
class database
{
function __construct($pdo)
{
$this->pdo = $pdo;
}
function getData()
{
$query = $this->pdo->prepare('SELECT * FROM database');
$query->execute();
return $query->fetchAll();
}
}
?>
index.php:
<?php
require_once 'config.php';
require_once 'database.class.php';
$db = new database($pdo);
$rows = $db->getData();
?>
It is possible to improve the way you connect to databases using autoloading and dependency injection containers. Here is a way of using Auryn to connect to your database while being sure there is only one connection opened and not having to manually require files throughout your application.
I will cover only PDO and Auryn here. There are other dependency injection containers and notably the mysqli extension to connect to database, but the content should help you using another container if you wish to do so.
The database class
Having a database class is superfluous. The \PDO class is already providing all necessary methods to query the database. Having a database class makes you repeat the functions it provides and limits your actions (or makes you create many functions) when you want to for example use multiple different fetch styles depending on your needs in a specific method.
Dependency Injection
If you haven't already, have a read on dependency injection. The point is that when a class needs to access the database, it should not have to bother constructing the \PDO object, it should be constructed with it:
class Mapper {
private $pdo;
public function __construct(\PDO $pdo) {
$this->pdo = $pdo;
}
public function createFromId($id) {
$stmt = $this->pdo->prepare("SELECT name FROM foo WHERE id=:id");
$stmt->execute([
":id" => $id,
]);
return $stmt->fetchObject();
}
}
Notice that I directly pass the \PDO object, not a wrapper class. That way, I always have access to all of its capabilities, not only a subset of user-defined functions.
Dependency Injection Container
A dependency injection container helps build your classes, giving them the objects they need, and giving you great flexibility on how to actually build those objects. Here I'll only focus on configuring and sharing a \PDO object through the use of Auryn.
I assume you have installed the required Auryn class, the easier way is using composer. This is out of the scope of this answer, there are multiple resources on how to use it.
Create the injector
$injector = new \Auryn\Injector();
Define the \PDO class parameters
$injector->define("PDO", [
":dsn" => "mysql:host=localhost;charset=utf8;dbname=dbname",
":username" => "user",
":passwd" => "passwd",
":options" => [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
],
]);
You can either write the configuration parameters directly here, or get them from a config file. I like having a config.ini file and using parse_ini_file() to get my configuration options, as I can easily switch databases by editing a config file.
Share the \PDO object
$injector->share("PDO");
This part is really important. This lines makes the injector give the same \PDO object every time it is tasked with constructing a class that needs a connection. Note that the order of the lines is not important, you can share the class before defining it, only make sure to create you database needing classes after writing both those lines.
Create your objects
$mapper = $injector->make("Mapper");
That's it. The injector will create your mapper object, creating the \PDO object if it haven't already been, passing the existing instance if it has.
Autoloading
Assuming you have used composer, you can make use of its great autoloader. Otherwise you can also roll you own autoloader.
The point here is to stop having require() everywhere in your code, especially if you have complex class hierarchies, which you should have in a single responsibility compliant class system.
Wrapping up
With this set up, you now can use the \PDO object in your classes while being assured there will only be one instance per request, without the need to require files everywhere, and without using a singleton anti-pattern.
$database = new Connection();
class Connection
{
function __construct()
{
switch($_SERVER['DOCUMENT_ROOT'])
{
case 'path':
$this->host = 'hostname';
$this->user = 'username';
$this->passwd = 'password';
$this->database = 'dbname';
break;
default :
$this->host = 'localhost';
$this->user = 'root';
$this->passwd = 'root';
$this->database = 'dbname';
break;
}
$this->clink = #mysql_connect($this->host,$this->user,$this->passwd);
#mysql_select_db($this->database,$this->clink);
}
}
$objConn = new mysqlconnect();
$Conn = $objConn->setobjConnect("localhost","root","P#ssw0rd");
class Database{
var $last_query; //Saved result of the last query made
var $last_result; //Results of the last query made
var $func_call; //A textual description of the last query/get_row/get_var call
var $link; //database link
var $lastquery; //last query
var $result; //query result
// Connect to MySQL database
function database() {
$this->link=mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Server connexion not possible.');
//Set All Charsets to UTF8
mysql_query("SET character_set_results=utf8 , character_set_client=utf8 , character_set_connection=utf8 , character_set_database=utf8 , character_set_server=utf8");
mysql_select_db(DB_NAME) or die('Database connection not possible.');
}
/** Query the database.
* #param $query The query.
* #return The result of the query into $lastquery, to use with fetchNextObject().
*/
function query( $query ){
$this->lastquery=$query;
$this->result=#mysql_query( $query, $this->link );
return $this->result;
}
/** Do the same as query() but do not return nor store result.
* Should be used for INSERT, UPDATE, DELETE...
* #param $query The query.
* #param $debug If true, it output the query and the resulting table.
*/
function execute($query)
{
#mysql_query($query);
}
/** Convenient method for mysql_fetch_object().
* #param $result The ressource returned by query().
* #return An ARRAY representing a data row.
*/
function fetchArray($result){
if ($result == NULL)
$result = $this->result;
if ($result == NULL || mysql_num_rows($result) < 1)
return NULL;
else
return mysql_fetch_assoc($result);
}
/** Close the connecion with the database server.
* It's usually unneeded since PHP do it automatically at script end.
*/
function close()
{
mysql_close($this->link);
}
/** Get the number of rows of a query.
* #param $result The ressource returned by query(). If NULL, the last result returned by query() will be used.
* #return The number of rows of the query (0 or more).
*/
function numRows($result = NULL)
{
if ($result == NULL)
return #mysql_num_rows($this->result);
else
return mysql_num_rows($result);
}
}
![<?php
class mysqlconnect{
private $server=null;private $user=null;private $password=null;private $objConnect=null;
function setobjConnect($s,$u,$p){
$this->server = $s;
$this->user = $u;
$this->password = $p;
return $this->objConnect = mysql_connect($this->server,$this->user,$this->password);
}
}
$objConn = new mysqlconnect();
$Conn = $objConn->setobjConnect("localhost","root","P#ssw0rd");
if($Conn)
{
echo "Database Connect";
}
else
{
echo "Database Connect Failed.";
}
mysql_close($Conn);
?>]
1

PHP - multiple different databases dependency injected class

I've spent the last several hours trying to find an answer to the "best", most logical, etc way to write a php database class to simultaneously connect to one postgresql db and one mysql db. Also, I'd like to adopt a Dependency Injection design but am new to that whole concept.
So far I've come up with...
class Database {
public function PgSqlConnect() {
/* Connect to database */
$host = 'localhost';
$dbname = '---';
$user = '---';
$pass = '---';
$timeout = 5; /* seconds */
try {
$pgsql_dbh = new PDO("pgsql:host=$host; dbname=$dbname", $user, $pass);
$pgsql_dbh->setAttribute( PDO::ATTR_TIMEOUT, $timeout );
$pgsql_dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
return $pgsql_dbh;
} catch( PDOException $e ) {
echo 'Unable to connect to database: ' . $e->getMessage();
}
}
public function MySqlConnect() {
/* Connect to database */
$host = 'localhost';
$dbname = '---';
$user = '---';
$pass = '---';
$timeout = 5; /* seconds */
try {
$mysql_dbh = new PDO("mysql:host=$host; dbname=$dbname", $user, $pass);
$mysql_dbh->setAttribute( PDO::ATTR_TIMEOUT, $timeout );
$mysql_dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
return $mysql_dbh;
} catch( PDOException $e ) {
echo 'Unable to connect to database: ' . $e->getMessage();
}
}
}
Obviously the duplicated code violates the DRY approach. I know and have seen many examples of multiple db connections, but most deal with same driver and don't provide DI capability.
I should also add that I've considered placing the connection details into the Database class constructor as...
$driver = 'mysql';
...
$mysqldb = new Database($driver,$un,$pw,...);
$driver = 'pgsql';
...
$pgsqldb = new Database($driver,$un,$pw,...);
but I don't know if that is really a good idea nor how well it would work with DI.
Many thanks!
You should create an interface first for all the DB operations.
interface IDatabase
{
function connect();
function query();
...
}
Then have different driver classes implementing this interface
class MySQLDB implements IDatabase
{
}
class PGSQLDB implements IDatabase
{
}
This way you can easily use dependency injection.
class Test
{
private $db;
function __construct(IDatabase $db)
{
$this->db = $db;
}
}
You can call it as:
$mysqldb = new MySQLDB();
$test = new Test($mysqldb);
or
$pgsqldb = new PGSQLDB();
$test = new Test($pgsqldb);
To avoid duplicated code you can extend an abstract class
abstract class AbstractDb {
public function connect() {
// common code to avoid duplication
echo 'connected!';
}
abstract public function escapeField();
abstract public function escapeValue();
}
class MySQL extends AbstractDb {
public function escapeField() {
// Db-specific method
}
public function escapeValue() {
// Db-specific method
}
}
$db = new MySQL;
$db->connect();
Or use composition, and let the Db class use a different driver for db-specific methods
interface IDriver {
public function escapeField();
public function escapeValue();
}
class MySQLDriver implements IDriver {
public function escapeField() {
// Db-specific method
}
public function escapeValue() {
// Db-specific method
}
}
class Db {
public function __construct($driver) {
$this->driver = $driver;
}
public function connect() {
// common code here ? idk, it's just an example
echo 'connect!';
}
// this method is db-specific, so we call the driver
public function escapeField($field) {
return $this->driver->escapeField($field);
}
public function escapeValue() {
// same here
}
}
$db = new Db(new MySQLDriver);
$db->connect();
In php 5.4 there will be traits, so there will be more approaches to avoid code duplication.

Nested class in php 5

I want to use a class inside other class.
This is my first class:
class mydb {
public function connect($host="localhost",$user,$pass,$data,$persistency=False) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->data = $data;
$this->persistency = $persistency;
$this->link=#mysql_connect($this->host,$this->user,$this->pass)or die(mysql_error());
If(($this->link) AND ($this->data)) {
If(#mysql_select_db($this->data,$this->link) or die(mysql_error())) {
return True;
}
}
return False;
}
public function query($query="",$sup="") {
If($sup) {
$query=#mysql_query($query,$this->link)or die("<br /><br />".mysql_error()."<br /><br />");
} Else {
$query=#mysql_query($query,$this->link) or die("<br /><br />".mysql_error()."<br /><br />");
}
return($query);
}
}
This is my second class:
class articles {
function getpath($id) {
$sql=$db->query("select id,name from articles where id='$id'");
$row=mysql_fetch_array($sql);
return $row['name'];
}
}
I receive this error: ( Fatal error: Call to a member function query() )
You didn't create $db anywhere.
You need to create reference to the mydb class in the articles class. Assuming you create an instance of the mydb class and stored it in the variable $db, this is what you would do.
class articles {
public static $db = null;
...
self::$db->query(...);
}
articles::$db = $db;
The variable doesn't have to be static, it could be a regular class variable. You would then reference it like
$this->db->query(...);
As has been mentioned, you need an instance of the mydb class made available to articles.
Here's a simple example (an anti-pattern, perhaps):
class articles {
private $db = null; // add a class property to hold your database object
// each time we create an articles object, we'll connect to the database
public function __construct() {
$this->db = new mydb(); // add your connection params
}
public function getpath($id) {
// now we can reference the object via the class property $this->db
$sql = $this->db->query("select id,name from articles where id='$id'");
$row = mysql_fetch_array($sql);
return $row['name'];
}
// the rest of your code goes here
}
If you don't want to have to constantly create new mydb instances you can do something slightly different:
Dependency Injection
class articles {
private $db = null;
// Here we'll use an existing mydb object and pass it into our new articles
public function __construct(mydb $db) {
$this->db = $db;
}
// the rest of your code goes here
}
$db = new mydb(); // create mydb
$db->connect(); // add your params
$articles = new articles($db); // create articles using mydb from above
Singleton
class mydb {
// This is static, which is special
private static $instance = null;
public static function setInstance(mydb $db) {
self::$instance = $db;
}
public static function getInstance(mydb $db) {
return self::$instance;
}
// the rest of your code goes here
}
$db = new mydb(); // Create a mydb object
$db->connect(); // add your params
mydb::setInstance($db); // store the mydb object in the mydb class
// Later when you want to query we use the stored object:
// (and this will now work everywhere in the program--
// we don't need $db anymore)
mydb::getInstance()->query();
Analysis
Generally, you don't want to open or maintain many connections to your database. It's faster to use the same connection everywhere. Sometimes this is not appropriate, though.
The singleton is a cheap form of a Service Locator.. I.e. you create a resource (or "service") and then you have a means to locate that resource without passing a variable around with you. Singletons are a basic fix, but there are better ways to architect your applications.
You might want to read about two relevant design patterns: Service Locator and Dependency Injection. Both are described here: http://martinfowler.com/articles/injection.html

Categories