0

this is my DataBase Class

class DataBase{
  public $_localhost  = "localhost";// server name => usually is localhost
  public $_user       = "root"; // username for the database
  public $_password   = "123"; // password for the database
  public $_dbname     = "ecommerce"; // database name
  public $db          = false;

  public  function __construct(){
    try{
      $this->db = new PDO("mysql:host=".$this->_localhost.";dbname=".$this->_dbname,$this->_user,$this->_password);
      $this->db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
      $this->db->exec("SET NAMES utf8");
      return $this->db;
    }catch(PDOException $e){
      echo $e->getMessage();
    }
  }

  public function __destruct(){
    $this->db = null;
  }
}

Now in my Application Class

class Application{
  public $db;

  public function __construct(){
    $this->db =  new DataBase();
    $this->db = $this->db->db;
  }
}

As i want to use my destruct function to close my connection

Is that a good way to do this ?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Hasan Zohdy
  • 101
  • 2
  • 6
  • 1
    The question is: why? – PeeHaa Jun 20 '14 at 19:55
  • Just a sidenote: setting an instance variable to `null`, in the destructor, is a no-op. – Michael Jun 20 '14 at 19:56
  • I'd say no, mostly because the [destructor is not always called](http://stackoverflow.com/questions/2385047/when-will-destruct-not-be-called-in-php) – Jeff Lambert Jun 20 '14 at 20:00
  • You should not create a new database connection for each instance you create. Pass the connection object around. (Keywords: Dependency Injection). Anyway. Your code does not really add any value. PHP will handle the connection itself, after the request was sent. Closing a connection by hand is mostly required if you have a long running process or use some insane database drivers(not the case here). – Rangad Jun 20 '14 at 20:02
  • @watcher well, that's not a problem here because the DB connection is always destroyed on shutdown. and as long as the script is in the normal run-time phase and as long as there are no retain cycles, destructor behaviour is deterministic... – Michael Jun 20 '14 at 20:02
  • So what is the best practice to close my connection automatically after my sql executions ? – Hasan Zohdy Jun 20 '14 at 20:03
  • @Rangad My Application class would be my main class for my project – Hasan Zohdy Jun 20 '14 at 20:04
  • @HasanZohdy the connection closed automatically when your script ends. If you really need to close the connection manually, there is usually a `close` method for that... – Michael Jun 20 '14 at 20:04
  • @Michael so you mean i don't have to close it myself ? – Hasan Zohdy Jun 20 '14 at 20:05
  • @HasanZohdy well, what do you think? how long does the script run? is it a script that gets executed by a webserver for serving a web-request? is it a command line tool? or is it a batch processing application? – Michael Jun 20 '14 at 20:07
  • @Michael aha, i got it , one last more question , is it good to instantiate my database class in application class construct ? – Hasan Zohdy Jun 20 '14 at 20:16
  • See my answer below. Or go read on some of the better php dependency injection examples and tutorials around the net. – Rangad Jun 20 '14 at 20:17

1 Answers1

4

You do not need to close the database connection in a typical php flow. The PHP runtime will do that itself once the response was returned and all references to the object are unreachable.

In many web-situations you want persistent connections anyway as they reduce the overhead of opening and closing a connection each time a request is sent to your site.

Check the documentation for PDO::ATTR_PRESISTENT(Example 4) for that

However, you might want to close the database connection yourself in a long running non-web process if you know that the connection will idle for several minutes of hours.

Set the PDO object to null in that case to signal PHP that this specific connection may be closed.

The destructor is the wrong place for that. Add a custom close method to your class that does not depend on PHP Runtime calls.

Btw.:

It is considered bad practice to create an unnessesary hard coupling between your Application instance and the database connection(-wrapper). I would suggest you to change your application class to allow a constructor injection of your database instance. This might really help you with testing/mocking your code later and improves reusability.

If you want to read more about the general concept in php and an implementation example see this series of blog entries by Fabien Potencier

Rangad
  • 2,110
  • 23
  • 29
  • Thanks , but can you give me an example about changing my application class to allow a constructor injection of your database instance ? – Hasan Zohdy Jun 20 '14 at 20:18
  • I added a link to a blog entry. You can simplify some of the examples there to suit your need and refer to the links in there for the more general concept. (Some of the linked material in the end of the entrie is really good, even if it's about java) – Rangad Jun 20 '14 at 20:26