Is it a good practice to use only one database instance? I want to ask whether is good practice to have single instance of database connection or declare new every times when we needed? I have this two designs shown in below:

This is database class:

&lt;?php
class Database extends PDO {
    //Variable declaration
    private $host = "localhost";
    private $passwd = "";
    private $username = "root";
    private $dbname = "";

    //Connect to DB when the class construct
    public function __construct($host=NULL, $dbname=NULL, $username=NULL, $passwd=NULL) {
        if(isset($host) &amp;&amp; !empty($host)) {
            $this-&gt;host = $host;
        }

        if(isset($dbname) &amp;&amp; !empty($dbname)) {
            $this-&gt;dbname = $dbname;
        }

        if(isset($username) &amp;&amp; !empty($username)) {
            $this-&gt;username = $username;
        }

        if(isset($passwd) &amp;&amp; !empty($passwd)) {
            $this-&gt;passwd = $passwd;
        }

        parent::__construct("mysql:dbname=$this-&gt;dbname;host=$this-&gt;host", $this-&gt;username, $this-&gt;passwd, NULL);
        parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        parent::setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    }

}
?&gt;


Method 1: Dependency Injection

&lt;?php
class User {
    private $db;

    public function __construct(Database $db) {
        $this-&gt;db = $db;
    }

    //Some update and insert below here
}

class Employee {
    private $db;

    public function __construct(Database $db) {
        $this-&gt;db = $db;
    }

    //Some update and insert below here
}
?&gt;


So, I will do this when I want to use the classes:

&lt;?php 
    $db = new Database();

    $user = new User($db);
    $user-&gt;update($id,$data);

    $emp = new Employee($db);
    $emp-&gt;delete($id);
?&gt;


or the other ways?

Method 2:

&lt;?php
class User {
    private $db;

    public function __construct() {
        $this-&gt;db = new Database();
    }

    //Some update and insert below here
}

class Employee {
    private $db;

    public function __construct() {
        $this-&gt;db = new Database();
    }

    //Some update and insert below here
}
?&gt;


and when I want to use it I will:

&lt;?php  
    $user = new User();
    $user-&gt;update($id,$data);

    $emp = new Employee();
    $emp-&gt;delete($id);
?&gt;


Which method is preferable or better?  Offer an explanation if possible.
In this situation, I would say to prefer dependency injection. The reasons being:


You may not only ever run your code against a single database. For example, if your project grows large, you may have separate databases for different regions about the world (North America, Europe, etc). You could also easily have a test database, or a stable-test and an experimental-test, etc., etc. All of this can be controlled by configuration. Of course, you could put this configuration-based logic inside your Database class, but..
You may want different types of databases. Right now you might be using MySQL; later you may use Oracle, or PostgreSQL, or an in-memory database, or a mock database that has no backing store. Using dependency injection lets you very easily switch between these, provided you adhere to the same interface for all of them. You may even start in one region where MySQL is readily available, then expand to another where you can only get Oracle hosts. Changing types is useful for easily changing technologies later, but also because..
Using dependency injection makes it easier to test. You can test against an in-memory database for your unit tests, and against an actual remote database during integration testing.


Of course, dependency injection isn't a cure-all and has its own problems, such as:


Dependency injection is infectious. Somebody somewhere has to know how to construct your object. If your object has its dependencies injected, then it has to know how to construct the dependencies. If it doesn't know that, then the next level up has to know, or the next level from there, or.. etc, etc. The result of this is that..
Your classes become more tightly coupled. Some of your classes become aware of the dependencies of their other classes, by virtue of having to inject their dependencies. A good dependency injection framework can help with this. I don't use PHP much so I'm not aware of any.


This is Code Review, of course, and I'd be remiss if I didn't comment on your code itself:

&lt;?php  
    $user = new User();
    $user-&gt;update($id,$data);

    $emp = new Employee();
    $emp-&gt;delete($id);
?&gt;


To me (admittedly a non-PHP guy) this is an extremely strange pattern. A User and an Employee would seem to be model classes, but here you have them making database calls - you're mixing concerns for these objects. This is a great situation to use the data access object, or DAO, pattern:

&lt;?php
    class User {
        private $data;

        public function __construct() {
            $data = "some_data";
        }
    }

    interface UserDao {
        public function saveUser(User $user);
    }

    // A DAO implementation which knows how to communicate using
    // PDO objects. Can be injected with a PDO object configured
    // to connect to any host, DBMS, etc.
    class PDOUserDao implements UserDao {
        private $pdo;

        public function __construct(PDO $pdo) {
            $this-&gt;pdo = $pdo;
        }

        public function saveUser(User $user) {
            // insert pdo insert/update logic here
        }
    }

    class InMemoryUserDao implements UserDao {
        // in-memory implementation here..
    }
?&gt;