OO Problem / Using Singleton Pattern / Extending mysqli

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxx>.
*
* 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@xxxxxxxxxxx>.
*
* 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 --

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux