Since PDOStatement::execute
returns true/false and your current run
method is returning a PDOStatement
on success and false on failure. I suggest checking prepare
and execute
are not false and return the PDOStatement
on success or false otherwise, as is the case with PDO::prepare
and PDO::query
.
/**
* @return PDOStatement|false
*/
public function run($sql, $args = [])
{
if (!$args) {
return $this->pdo->query($sql);
}
if ($stmt = $this->pdo->prepare($sql)) {
if ($stmt->execute($args)) {
return $stmt;
}
}
return false; //either prepare or execute failed
}
$db = Database::instance();
var_dump($db->run('SELECT ?', ['foo', 'bar'])); //false
An alternative approach would be to store the last execute value in a property, for later retrieval.
class Database
{
protected $lastExecute;
//...
/**
* @return PDOStatement|false
*/
public function run($sql, $args = [])
{
if (!$args) {
return $this->pdo->query($sql);
}
if ($stmt = $this->pdo->prepare($sql)) {
$this->lastExecute = $stmt->execute($args);
}
return $stmt;
}
/**
* @return null|bool
*/
public function getLastExecute()
{
return $this->lastExecute;
}
}
$db = Database::instance();
$db->run('SELECT ?', ['foo', 'bar']);
var_dump($db->getLastExecute()); //false
To address the best-practices comments below in regard to the issue of determining when PDO::execute
method specifically fails from within the Database::run
method by using Exception handling.
Please keep in mind Best-Practices are not about right or wrong, "they are simply recommended methods of writing code." Referring to an approach of programming that is commonly preferred in professional application development. Always use what works best for you, the environment you are developing for and your application requirements.
Generally speaking StackOverlow is not an appropriate place to discuss or evaluate an author's application of
best-practices. Those types of discussions or critiques should be reserved for CodeReview. StackOverflow is
intended to answer the author's specific
question, or provide a viable alternative method to accomplish what the user is asking for. Not infer the user has asked the wrong question.
To use exceptions you need to enable PDO::ERRMODE_EXCEPTION
(see below Database
class).
The issue with using try/catch
with a PDO wrapper method, is that PDO will only throw a single Exception object PDOException
, which does not provide you with the ability to determine which PDO method call specifically failed. Leaving you to read the PDOException::getMessage()
or PDOException::getTrace()
, to determine the cause.
A simple approach would be to check the PDOException::trace
for the function name that caused the exception.
try {
$db = Database::instance();
var_dump($db->run('SELECT ?', ['foo', 'bar'])->fetch());
} catch(\PDOException $e) {
if ('execute' === $e->getTrace()[0]['function']) {
echo 'PDO::execute() failed';
//Handle the execute exception
}
throw $e;
}
Please see the answer on PDO mysql: How to know if insert was
successful by Your
Common Sense for a more generalized approach to PDOException
handling.
The above approach prevents you from handling only specific exception types within the Database::run
method, requiring you to use throw $e;
after your conditional, when the exception is unexpected.
To account for this issue, another approach would be to create custom Exception classes. You can do this by extending the base PDOException
class to be compliant with other exception handling methods or to catch any of them.
In order to catch any of the run
specific exceptions, an empty interface can be used that is then implemented on the extended PDOException
classes.
interface DatabaseRunException{}
Then create a new exception class for each of the specific PDO methods you would like to handle, that implements the DatabaseRunException
interface.
class PDOPrepareException extends PDOException implements DatabaseRunException{}
class PDOExecuteException extends PDOException implements DatabaseRunException{}
class PDOQueryException extends PDOException implements DatabaseRunException{}
To use the custom exceptions to determine which PDO method failed, you need to handle the PDOException(s)
within the Database::run()
method and throw
one of the custom exceptions.
I have removed certain portions for brevity, commented out things that would alter your current configuration, made some best-practices and optimization changes for PHP 5.6+.
class Database
{
//...
protected function __construct()
{
//static reduces overhead
static $opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
// PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
// PDO::ATTR_EMULATE_PREPARES => false
];
$this->pdo = new PDO($dsn, self::user, self::password, $opt);
}
public static function instance()
{
if (null === self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
public function __call($method, $args)
{
//always ensure the desired method is callable!
if (is_callable([$this->pdo, $method])) {
//php 5.6+ variadic optimization (aka splat operator)
return $this->pdo->$method(...$args);
//php <= 5.5
//return call_user_func_array(array($this->pdo, $method), $args);
}
throw new \BadMethodCallException(sprintf('Unknown method PDO::%s called!', $method));
}
public function run($sql, $args = [])
{
if (!$args) {
try {
return $this->query($sql);
} catch(\PDOException $e) {
throw new \PDOQueryException($e->getMessage(), (int) $e->getCode(), $e);
}
}
try {
$stmt = $this->prepare($sql);
} catch(\PDOException $e) {
throw new \PDOPrepareException($e->getMessage(), (int) $e->getCode(), $e);
}
try {
$stmt->execute($args);
} catch(\PDOException $e) {
throw new \PDOExecuteException($e->getMessage(), (int) $e->getCode(), $e);
}
return $stmt;
}
}
You will now be able to handle each of the exceptions for any of the specific types.
try {
$db = Database::instance();
$db->run('SELECT ?', ['foo', 'bar']);
} catch(\PDOExecuteException $e) {
echo 'PDO::execute() failed';
//Handle the execute exception
throw $e;
}
In PHP 7.1+ you can catch multiple exceptions.
try {
$db = Database::instance();
$db->run('SELECT ?', ['foo', 'bar']);
} catch(\PDOQueryException $e) {
//Handle the query exception
throw $e;
} catch(\PDOPrepareException $e) {
//Handle the prepare exception
throw $e;
} catch(\PDOExecuteException $e) {
echo 'PDO::execute() failed';
//Handle the execute exception
throw $e;
}
In PHP <= 7.0 you can use the DatabaseRunException
interface to catch and check the specific exceptions caused by Database::run()
with instanceof
to determine which exception was actually thrown.
try {
$db = Database::instance();
$db->run('SELECT ?', ['foo', 'bar']);
} catch(\DatabaseRunException $e) {
if ($e instanceof \PDOQueryException) {
//Handle the query exception
} elseif ($e instanceof \PDOPrepareException) {
//Handle the prepare exception
} elseif ($e instanceof \PDOExecuteException) {
echo 'PDO::execute() failed';
//Handle the execute exception
}
throw $e;
}
As you can see, this increases the code complexity and will be up to you on determining what best suits your application needs.
It is important to note, that variables declared in the try
section will not be declared if an exception occurs prior to the declaration.
try {
throw new \Exception('FooBar');
$foo = 'foo';
} catch(\Exception $e) {
var_dump(isset($foo)); //false
}
var_dump(isset($foo)); //false