This is not a good class. I am not going to get into everything that is not right. Take a look at the new clean-pdo repo I put up for a starting point.
Haven’t we been here before?
I seem to recall you posting a similar class to this a while ago. If not, one quite similar.
This looks as useless for all the same reasons.
Pretty much every method, just duplicates what PDO already does in its native methods, only with very limited functionality.
You may as well just use vanilla PDO than restrict yourself with this.
The real power of PHP OOP comes not from PDO connection as that is already in Object-Oriented Programming style if you think about it. Rather it comes from the ability to do more than one thing with a class. I’m in the process of redoing my website(s) using the Active Record Design Pattern where I will be able to use the DatabaseObject class:
class DatabaseObject extends Database
{
static protected string $table = "";
static protected array $columns = [];
public static function fetch_all(): array
{
$query = "SELECT * FROM " . static::$table;
$stmt = self::pdo()->query($query);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public static function fetch_by_id($id): array
{
$query = "SELECT " . implode(", ", static::$db_columns) . " FROM " . static::$table . " WHERE id=:id LIMIT 1";
$stmt = self::pdo()->prepare($query);
$stmt->execute(['id' => $id]);
return $stmt->fetch(PDO::FETCH_ASSOC);
}
protected static function create():bool
{
// Still working on it.
}
Where I will be able to use it for a cms, login and many other applications for my website. I highly recommend getting used to working with OOP first especially inheritance as once you get that down a light bulb should turn on by making silly classes like animals then narrowing it down to cats, dogs or fish as an example (There are many other examples you could do like vehicles). Once you get that down pat then look at Active Record Design pattern or many other types of patterns that are out there that will suit your need.
Please don’t. Active Record is nice for Rapid Application Development where you want a prototype fast, but should not be used for anything serious. Main reason being it’s going against Single Responsibility Principle in a big way.
If you want a wrapper around PDO (which is always a good thing), I would recommend Doctrine DBAL.
Stuff like this has been written over and over, and Doctrine DBAL is very mature and stable. No need to re-invent the wheel. Use your time on solving actual business problems, instead of technical ones that have already been solved.
Not to pile on but inheritance is one of the least useful aspects of OOP and should be used with caution. All of those silly tutorials using animals tend to come crashing down as soon as you realize that birds, bats and insects can all fly.