help with singleton mysqli database object in extended class
- From: rdlowrey <rdlowrey@xxxxxxxxx>
- Date: Wed, 28 Jan 2009 16:06:54 -0800 (PST)
The last line of the code below is my problem -- when I instantiate
the Child object, Base::writeData() runs fine and performs my query,
but then the Child::writeData() function does not execute a query.
Everything works fine except I cannot query the database from within
the Child extended class because it can't seem to access the $db
object. I've tried overriding the Base::__construct() and fiddling
with member and method overrides and scopes ad nauseum. PLEASE take a
look at the code below and HELP ME. Many thanks :-)
class Database{
protected static $db;
private function __construct(){}
public static function getInstance(){
if ( !isset(self::$db) ){
self::$db = new mysqli('host', 'user', 'pass', 'db');
}
return self::$db;
}
}
class Base{
protected static $db;
function __construct(){
$this->db = Database::getInstance();
}
function writeData($data){
$query = "..."
$this->db->query($query)
}
}
class Child extends Base{
function writeData($data){
$query = "..."
$this->db->query($query)
}
}
$test = new Child();
$test->writeData($data);
.
- Follow-Ups:
- Re: help with singleton mysqli database object in extended class
- From: "Álvaro G. Vicario"
- Re: help with singleton mysqli database object in extended class
- From: amygdala
- Re: help with singleton mysqli database object in extended class
- From: Michael Fesser
- Re: help with singleton mysqli database object in extended class
- Prev by Date: Send double set of emails from form
- Next by Date: Re: sub-array indexing
- Previous by thread: Send double set of emails from form
- Next by thread: Re: help with singleton mysqli database object in extended class
- Index(es):
Relevant Pages
|