Object oriented php,class members' scope

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.

As you’ve discovered, it’s all about scope. Maybe this will help you on your way. :wink:


<?php
class Database
{
    protected
        static $instance = null;
        
    protected
        $conn;
        
    
    private function __construct(){
        $this->conn = mysql_pconnect('localhost', 'username', 'password');
        mysql_select_db('database', $this->conn);
    }
    
    public static function getInstance(){
        if(true === is_null(self::$instance)){
            self::$instance = new Database();
        }
        return self::$instance;
    }
    
    public function execute($query){
        $result = mysql_query($query, $this->conn);
        if(true === is_resource($result)){
            return new Database_Result($result);
        }
        return (bool)$result;
    }
}

class Database_Result
{
    protected
        $result;
        
    public function __construct($result){
        $this->result = $result;
    }
    
    public function getNumRows(){
        return mysql_numrows($this->result);
    }
}

$database = Database::getInstance();
echo $database->execute('SELECT * FROM table LIMIT 10')->getNumRows(); #10

?>

  1. connectDB() should return the database connection. It just assigns it to a variable that stays inside the function.

Thanks guys. Here is what I changed and it worked.

Made all the member-variables in the class private. Changed the constructor in the class to the following;

// Private constructor to limit object instantiation to within the class	
	private function __construct() { 
		$this->db_server="localhost";
		$this->db_user="root";
		$this->db_password="";
		$this->db_database="dbname";  

		$this->dbs_link = mysql_pconnect($this->db_server, $this->db_user, $this->db_password);
		$this->db_link = mysql_select_db($this->db_database, $this->dbs_link);
	} 

Put the following function in the class;

// method to get a reference to the DB server resource
	public function getDbsLink()
	{
		return $this->dbs_link;
	}  

Changed the connectDB() function

function connectDB()
{
	return Database::getInstance();

}

and then,


...
$dbInstance = connectDB();
...
$rset = mysql_query($mq, $dbInstance->getDbsLink());
...