database parameters on function __construct

I am wondering if my understanding is right.

I placed my database connection parameter on my __construct(), so this means every time the class is instantiated, I also reconnect to my database, right?

private $handle;
public function __construct()
{
  $username = "test";
  $password = "9712nc*<?12";
  $host = "localhost";
  $db = "miner";
  $dsn = 'mysql:dbname='.$db.';host='.$host;
  try {
    $this->handle = new PDO($dsn,$username,$password);
  }
  catch(PDOException $e) {
    print $e->getMessage();
    die();
  }
}

Is this a good practice if I have many request from a certain user? Does this mean every time a user make a request (even if the request is just done minutes ago) the script should first connect to the database? Is there any appropriate way I can preserve my $handle?

BTW: The database connection is working fine.

Leave a Reply

*

Hire Me
Follow Me!
Search
Most Popular Articles & Pages
Because your vote is Important
Sorry, there are no polls available at the moment.
Categories