I created a derived class to have a more self-documented instruction instead of $conn=null;
.
class CMyPDO extends PDO {
public function __construct($dsn, $username = null, $password = null, array $options = null) {
parent::__construct($dsn, $username, $password, $options);
}
static function getNewConnection() {
$conn=null;
try {
$conn = new CMyPDO("mysql:host=$host;dbname=$dbname",$user,$pass);
}
catch (PDOException $exc) {
echo $exc->getMessage();
}
return $conn;
}
static function closeConnection(&$conn) {
$conn=null;
}
}
So I can call my code between:
$conn=CMyPDO::getNewConnection();
// my code
CMyPDO::closeConnection($conn);