2

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 .

Bushra Shahid
  • 781
  • 7
  • 20
  • 1
    Your immediate problem is likely **`include_once`**! Your bigger problem is that this is bad OO structuring. Your `Functions` class *is not* a `Connection`, so it should not `extend Connection`. You're also instantiating a new database connection with every one of your subclasses, which is wasteful. Look into *composition* and *dependency injection* over inheritance. – deceze May 25 '15 at 10:41

1 Answers1

2

You're missing the return keyword

class Functions extends Connection {
    private $conOb;

    function __construct() {
        parent::__construct();
        $this->conOb = parent::getConnectionObject();
    }

    function getConnectionObject() {
        return parent::getConnectionObject();
    }

}

By the way, you don't have to redeclare the method getConnectionObject() if you don't add anything to it.

You can simply do :

class Functions extends Connection {

    function __construct() {
        parent::__construct();
    }

}

$db = new Functions();
$dbh = $db->getConnectionObject();

And if you change the visibility of the property $conn to protected in the class Connection, you can use your connection in subclasses like that :

class Functions extends Connection {

    function __construct() {
        parent::__construct();
    }

    function doSomething() {
        $this->conn->query('SELECT....');
    }

}

As a side note, i encourage you to take a look at the PHP coding style standards.

Alfwed
  • 3,307
  • 2
  • 18
  • 20
  • actually i want to store this connection object for further use in my Function class.. i am doing my database operations here. – Bushra Shahid May 25 '15 at 09:54
  • i appreciate your suggestions n hard work but my problem is rising from connection class. if i set my variables in this class like ` private $host = "localhost"; private $user = "root"; private $password = ""; private $database = "mydb"; protected $conn;` without inherit config class. I am able to do every thing. – Bushra Shahid May 25 '15 at 10:09
  • So what's the problem with it? – Alfwed May 25 '15 at 10:12
  • facing same problem. my connection object is null and i am not able to use it to perform database operations. – Bushra Shahid May 25 '15 at 10:13
  • i review my code and var_dump $config it showing me null.. Not understanding why is null. – Bushra Shahid May 25 '15 at 10:36