I have a configuration file where I am defining my database configuration. My configuration file is
<?php
$config['database']="mydb";
$config['host']="localhost";
$config['username']="root";
$config['password']="";
?>
I have a config class where I am assigning my configuration settings my config class is like
class Config {
//put your code here
protected $host = "";
protected $user = "";
protected $password = "";
protected $database = "";
protected function __construct(){
include_once 'configuration.php';
$this->host=$config['host'];
$this->user=$config['username'];
$this->password=$config['password'];
$this->database=$config['database'];
}
}
now I am trying to establish my database connection to my connection class like
include_once 'Config.php';
class Connection extends Config{
private $conn;
function __construct() {
parent::__construct();
try {
$this->conn = new PDO("mysql:host=$this->host;dbname=$this->database", $this->user, $this->password);
} catch (PDOException $pe) {
echo "Error connecting to database. " . $pe->getMessage();
}
}
function getConnectionObject() {
return $this->conn;
}
public function destroyConn() {
$this->conn = null;
}
}
My problem is when I am trying to get this connection for further class it showing me blank object My code for access database connection object is
class Functions extends Connection {
private $conOb;
function __construct() {
parent::__construct();
$this->conOb = parent::getConnectionObject();
}
function getConnectionObject() {
parent::getConnectionObject();
}
}
if I am defining my database configuration in my connection class I am able to Access my connection object in my Function class but if I am trying to set it by configuration file getting Null connection object.
Please let me know where I am making mistake. Thanks in advance .