I am just starting to use OOP in php, i have a little piece of code here, just trying to connect to a DB using a singleton class and execute a query. I couldn’t access a member of the singleton class from outside of it, the member is declared public though.
.php file #1;
<?php
class Database
{
// Store the single instance of Database
private static $mpInstance;
private $db_server; private $db_user;
private $db_password;
private $db_database;
public $dbs_link;
public $db_link;
// Private constructor to limit object instantiation to within the class
private function __construct() {
$db_server="localhost";
$db_user="root";
$db_password="";
$db_database="";
$dbs_link = mysql_pconnect($db_server, $db_user, $db_password);
$db_link = mysql_select_db($db_database, $dbs_link);
}
// Getter method for creating/returning the single instance of this class
public static function getInstance()
{
if (!self::$mpInstance)
{
self::$mpInstance = new Database();
}
return self::$mpInstance;
}
}
//////////////////////////////////////////////////////////
// Wrap the database class instantiating code in a function
function connectDB()
{
// Get the single instance of the Database class using the gettor
// method we created.
$pDatabase = Database::getInstance();
}
?>
.php file #2;
<?php
require_once("includes/dbconnect.php");
connectDB();
$mq = "SHOW COLUMNS FROM bill_1";
$rset = mysql_query($mq, Database::$dbs_link);
//$rset = mysql_query($mq);
while( $row = mysql_fetch_array($rset) )
print_r($row);
?>
The error message says: Access to undeclared static property:Database::$dbs_link …
That mysql_query(…) line works if run without the second argument,but I would like to know why that member is not accessible. Where am I going wrong,any thoughts?.
thanks in advance.