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.
If the class is instantiated once, then you’re going to be just fine. In this case, you open one connection only.
If you instantiate the class several times, or use it as a static class, then you’re potentially going to create a connection each time, which is not ideal.
If you keep the classes all active (i.e. you never delete reference to a class once you create it, from memory (I’ve never tested it) the internals of PHP should actually sort this out for you, and you’ll still only have the one connection to the database. But if you lost a handle to the class you created, then the PDO will be destroyed and you start again. If you are using as a static class (i.e. you call it with
class:function()), then you’re on the wrong path.There are two common solutions, and you’ll find arguements for and against both of them.
1) Use a global for storing your DB connection. You create the $handle = PDO and store $handle as a global variable. Easy to pass around. Easy to overwrite – I’m not going to debate here.
2) Create a “static” class (commonly called a Singleton) that you recall. The basic structure would be
Using this, you would normally create a separate DB class as a singleton. Using Singleton vs Global: your call (after research).
3) The third answer is if you are only ever calling your class above as a static (i.e. you call
class::function()) then you can just make the $handle as a static and actually your problems should be wonderously solved. Essentially you’re creating a Singleton, with the database as one of the properties.Sorry that there’s no wrong or right answer.
Edit based on comment “What’s a static class”:
Strictly speaking, I should say “class with static properties”. To explain, normally you create a class with the syntax:
and then do lots of nifty little things with
$myClassby calling functions. You can then drop reference to the class, lose the properties (variables) and will reinitilaise later on.A static class (for the purposes of this answer) is one where you have static properties. Normally (not absolutely) they are called by going.
You don’t hold on to the class – it gets initiated, used and dropped. However, if you store properties (
vars) as static then each time you use that class, those properties will still exist in the state they last were. Somethodcan set$this->MyVar = 'something'and next time, $this->MyVar will still be something.Very useful for compartmentalising your code, stops functions overwriting others and can make unit testing easier.
(Note – I’m generalising to make it simpler. But it should give you an idea of the processes.)