I am trying to convert myself from using mysql extension to using PDO, with that being said I was curious if I am going about this the right way.
I have a custom logging system that I built I am wondering if the way I implemented this would work or not. I am planning on duplicating the main functions of PDO and doing the same as what I did prepare?
What I wanted to know is if the way I turned on exceptions and internally using try/catch blocks to capture and log errors then rethrowing the exception after I logged the error is the right way to do this or is there another more efficient way of doing this.
class rDB extends PDO {
private $dsn = "mysql:host=10.10.10.43";
private $user = "user";
private $pass = "pass";
/**
* @var Logger Writes messages to custom csv log based on debug level of 1-5
* 1 Being Critical Error, 5 Being a simple Note
*/
private $logger;
public function __construct($logger) {
$this->logger = $logger;
$this->logger->log(5, 'Connecting to MySQL DB');
try {
/* Call to Parent Function */
parent::__construct($this->dsn, $this->user, $this->pass);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->logger->log(5, 'Connected to MySQL DB');
}
catch(PDOException $e) {
$this->logger->log(2, 'Connection to MySQL DB Failed-> '.$e->getPrevious());
throw $e;
}
}
public function prepare($statement, array $driver_options = 'array()') {
try {
$this->logger->log(5, 'Preparing statement');
/* Call to Parent Function */
$value = parent::prepare($statement, $driver_options);
}
catch(PDOException $e) {
$this->logger->log(2, 'Error preparing statement-> '.$e->getMessage());
throw $e;
}
$this->logger->log(5, 'Statement is prepared.');
return $value;
}
}