Close PDO connection


I have worked with other languages before working with PHP and in some the framework when you opened a connection with the bank it was good to finish it in the PDO so I realized there is no function in the PDO class that closes this connection. Is there any way in the PDO to close the connection with the database?

asked by anonymous 19.05.2014 / 15:18

2 answers


Look, simpler impossible:

$pdo = null;

Of course this is not necessary because the connection is closed automatically after the script is executed

19.05.2014 / 15:20

Although it does not have much relation with the original doubt, it is worth commenting.

As you said to have seen in a framework, almost absolute certainty (goes that ...) that this was Object Oriented and in such cases an implementation of the PDO needs to have the property in which the instance of the PDO object itself destroyed so that the connection logic works by connecting only once.

The rough way is more or less like this:

abstract class Driver {

    protected $conn;

    public function getConnection() {

        if( $this -> conn === NULL ) {

             $this -> connect();

        return $this -> conn;

    abstract public function connect();

    abstract public function disconnect();

class PDO extends Driver {

    public function connect() {

        $this -> conn = new PDO( /** .. */ );

    public function disconnect() {

        $this -> conn = NULL;


Note: namespaces , interfaces, or even Object Oriented paradigms have not been taken in to simplify them as much as possible.

Thus, given the vertical and rising Requisite flow, a connection object defined in, say, an Application Controller can be used in an Action Controller or Model without risk of data loss.

And in the case of PDO at least it is very important since some of the operations are performed in the PDO and part in the PDOStatement and any undue re-connection between one step and another can, for example, generate an empty query error.

19.05.2014 / 19:06