OO Problem / Using Singleton Pattern / Extending mysqli
| From: | Lee PHP | Date: | Fri, 08 Jun 2007 22:12:18 +0000 |
| Subject: | OO Problem / Using Singleton Pattern / Extending mysqli | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-256405@lists.php.net to get a copy of this message | ||
Hi guys,
For the life of me I can't figure this out. I have two classes as follows:
1. Config.php (based on the Singleton pattern; gets/sets properties).
2. Database.php (also based on the Singleton pattern; extends mysqli).
-- BEGIN CLASS CONFIG --
<?php
class Config {
private static $INSTANCE;
private static $CONFIGFILE = "sys.cfg";
private $properties = array();
/** Constructor is private; getInsntace() will mediate object
* instantiation*/
private function __construct() {
// ...
}
/** Return the path to the configuration file.*/
public static function getConfigFile() {
return self::$CONFIGFILE;
}
/** Set the path to the configuration file.*/
public static function setConfigFile($file) {
self::$CONFIGFILE = $file;
}
/** Create or return an existant instance. */
public static function getInstance() {
if (empty(self::$INSTANCE)) {
self::$INSTANCE = new Config();
// Read the configuration file.
$lines = file(self::$CONFIGFILE);
foreach ($lines as $line_num => $line) {
if (ereg("^#", $line) || ereg("^[[:space:]]", $line)) {
// Ignore the comments and blank lines.
}
else { // Get properties from the file.
$tokens = explode("=", $line);
self::$INSTANCE->setProperty(trim($tokens[0]), trim($tokens[1]));
}
}
}
return self::$INSTANCE;
}
/** Set a property. */
public function setProperty($key, $val) {
$this->properties[$key] = $val;
}
/** Get a property. */
public function getProperty($key) {
return $this->properties[$key];
}
}
?>
-- END CLASS CONFIG --
-- BEGIN CLASS DATABASE --
<?php
/*
* Created on May 15, 2007
*
* Written/coded/hacked by Lee Reilly <lee.reilly@state.nm.us>.
*
* Extends PHP's MySQLi.
*/
require_once("Config.php");
class Database extends mysqli {
private static $CONFIG;
private static $INSTANCE;
public function __construct() {
}
public static function getConnection() {
if(empty(self::$INSTANCE)) {
self::$CONFIG = Config::getInstance(); // ERROR HERE
self::$INSTANCE = new Database();
}
return self::$INSTANCE;
}
}
?>
-- END CLASS DATABASE --
Now, in one of my unit tests, I call `$CONN =
Database::getConnection()` and receive an error message, "Fatal error:
Call to undefined method Config::getinstance() in
C:\xampp\htdocs\rsfs\php\classes\core\Database.php on line 23".
Can anyone tell me why the Database class can't see the Config class?
If it's any use, here's my unit test that's breaking:
-- BEGIN UNIT TESTS --
<?php
/*
* Created on June 8, 2007
*
* Written/coded/hacked by Lee Reilly <lee.reilly@state.nm.us>.
*
* Test class for Database class. */
require_once('../../external/SimpleTest/unit_tester.php');
require_once('../../external/SimpleTest/reporter.php');
require_once('../../classes/sys/Database.php');
class DatabaseTest extends UnitTestCase {
private static $CONN;
public function testConstructor() {
$CONN = Database::getConnection();
}
}
$test = &new DatabaseTest();
$test->run(new HtmlReporter());
?>
-- END UNIT TESTS --