tags:

views:

78

answers:

1
+2  Q: 

PHP OOP Problem

Hi, I have a following class in php -

class BaseDatabase
{
    private $host;
    private $user;
    private $password;
    private $database;      
    private $connection;
    private $IDatabase;    // This variable will point to an instance of a class
                          // which implements the "IDatabase" interface
    private $status;

    // Read all the configuration information from the "config" file
    public function __construct()
    {
        $this->host = DATABASE_SERVER;
        $this->user = DATABASE_USER;
        $this->password = DATABASE_PASSWORD;
        $this->database = DATABASE;
    }

    public function open_connection()
    {
        $this->connection = $this->IDatabase->open($this->host, $this->user, $this->password, $this->database);
    }

    public function fetch_single_object($query='')
    {
        $database_object = $this->IDatabase->fetch_single_row($this->connection, $query);

        return $database_object;
    }

    public function fetch_object_list($query='')
    {
        $database_object_list = $this->IDatabase->fetch_multi_row($this->connection, $query);

        return $database_object_list;
    }

    public function insert_data($query='')
    {
        $this->status = $this->IDatabase->insert($this->connection, $query);
    }

    public function update_data($query='')
    {
        $this->status = $this->IDatabase->update($this->connection, $query);
    }

    public function delete_data($query='')
    {
        $this->status = $this->IDatabase->delete($this->connection, $query);
    }

    public function call_procedure($query='')
    {
        $this->status = $this->IDatabase->execute_procedure($this->connection, $query);
    }

    public function close_connection()
    {
        $this->status = $this->IDatabase->close($this->connection);
    }

    public function get_status()
    {
        return $this->status;
    }

    public function set_database_behavior($IDatabase)
    {
        $this->IDatabase = $IDatabase;
    }

    public function __destruct()
    {
        $this->close_connection();
    }
}

The IDatabase interface is as follows-

interface IDatabase
{
    public function open($host='', $user='', $password='', $database='');
    public function fetch_single_row($connection=NULL, $query='');
    public function fetch_multi_row($connection=NULL, $query='');
    public function insert($connection=NULL, $query='');
    public function update($connection=NULL, $query='');
    public function delete($connection=NULL, $query='');
    public function execute_procedure($connection=NULL, $query='');
    public function close($connection=NULL);
}

I have another class called MysqlQuery which implements the IDatabase interface. Now, I am extending the BaseDatabase class to create a new class called Mysql, which is as follows -

class Mysql extends BaseDatabase
{
    public function __construct()
    {
        $this->IDatabase = new MysqlQuery();

        parent::__construct();
    }
}

But when I am running the following piece of code, I am getting a fatal error message -

$database_connection = new Mysql();

$database_connection->open_connection();
$database_connection->close_connection();

The error message is as follows -

Call to a member function open() on a non-object in C:\Workspace\Others\Others\Wamp\www\Generic Management System\modules\database\base_classes\BaseDatabase.class.php  on line 26

How can I solve this problem ?

+3  A: 

Your BaseDatabase::IDatabase should be defined as protected, not private, for it to be available from your Mysql extension.

Or you could use $this->set_database_behavior(new MysqlQuery()); instead of $this->IDatabase = new MysqlQuery(); in your Mysql::__construct()

Ivar Bonsaksen
Yup, it worked. Thank you very much. But I am having a new error. When "$database_connection->close_connection()" executes, it generates a warning, saying "mysql_close(): 7 is not a valid MySQL-Link resource in C:\Workspace\Others\Others\Wamp\www\Generic Management System\modules\database\concrete_classes\MysqlQuery.class.php". Could you tell me the reason?
Night Shade
Sorry, I found the answer. I was closing the same connection twice, in the destructor and in the "close_connection()" method. Than you again... :)
Night Shade