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 ?