Reputation: 2901
K I am passing a PDO connection to a class constructor to get some sql information. It works perfectly when I don't manually close the PDO with NULL.
try
{
$dbh = new PDO('mysql:host=localhost;dbname=test');
}
catch(PDOException $ex)
{
echo "Failed to connect to the database: " . $ex->getMessage();
}
$objGetReference = new getReference($dbh);
$reference=$objGetReference->getReference();
However, when I have the exact same code with a $dbh=null; at the end to close the pdo object it fails. So the following does not work:
try
{
$dbh = new PDO('mysql:host=localhost;dbname=test');
}
catch(PDOException $ex)
{
echo "Failed to connect to the database: " . $ex->getMessage();
}
$objGetReference = new getReference($dbh);
$reference=$objGetReference->getReference();
$dbh=NULL;
I know the php script will kill it anyway when it finishes, I want to end it like this though. Just good habit.
How do I close the database connection early?
Also if I pass it to multiple classes, should I close it individually inside each class that uses it?
Thanks all.
Upvotes: 0
Views: 406
Reputation: 24383
If you want to terminate the execution of the script, you can use die or exit with an optional error message that will be presented to the user.
According to the PDO manual:
To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the variable that holds the object
It is worded a bit wrong (at least to me), because not only do you need to destroy all references, but you also need to destroy all copies too.
For example:
$a = new PDO( [args] );
$b = $a;
$a = null;
Then $b
is still connected to the database, so you have to make sure that you destroy it as well. This is also true for using it as an argument of a method/function since this creates a copy of the original object instead of a reference (unless passed by reference):
function killDbh($dbh) {
$dbh = null;
}
killDbh($dbh);
echo get_class($dbh); // echoes PDO
In order to close the connection, you would need to close all connections, including any in objects. However, if you do,
$a = new PDO( [args] );
$b =& $a;
$a = null;
Then $b
is a reference of $a
and it will also be destroyed. Essentially, there is no true way to close the connection of a PDO object (like mysqli_close) aside from upon script termination because there is no way to set the object to a "disconnected" state, so any copies of the object are still connected.
In my experience, it is very seldomly necessary to explicitly close a database connection anyway, so you have little to worry about.
Upvotes: 1
Reputation: 562310
It should be fine to close a PDO connection in this way. You should describe more clearly what you mean by "does not work."
I would guess you are setting $dbh to null, but then trying to use it in a subsequent line of code.
To confirm this, I would make sure that my classes expecting a PDO object get one instead of null. For example, use a type hint:
class getReference {
public function __construct(PDO $dbh) {
...
}
}
This is easy to do, and should make the constructor throw a fatal error if you accidentally pass a null.
$dbh = null
$r = new getReference($dbh);
Here's the error:
Catchable fatal error: Argument 1 passed to getReference::__construct()
must be an instance of PDO, null given
Upvotes: 1