Why You Cannot Instantiate an Abstract Class and How to Properly Wrap Database Logic
The error Fatal error: Cannot instantiate abstract class my_database is one of the most common stumbling blocks for developers new to object-oriented design in PHP. It feels restrictive, but it serves a critical purpose: preventing you from creating incomplete objects that lack essential behavior. When you declare a class as abstract, you are explicitly telling PHP, "This blueprint is not a finished product; you cannot build an instance of it directly."
In your specific scenario, you have defined a base class database with a protected property $value. You then created an intermediate class my_database that extends this base class. The logic here is sound for inheritance, but the intention to instantiate my_database() directly violates the rules established by the abstract keyword. If my_database does not implement all methods inherited from its parent as concrete implementations, or if it declares any methods as abstract itself without providing an implementation in a child class, PHP will throw that fatal error.
The core issue you are facing is architectural rather than syntactic. You mentioned wanting to create a "wrapper class" to temporarily change the protected value. In PHP, a wrapper class usually implies composition or delegation, not just inheritance of an incomplete interface. If my_database is intended to be a standalone object that you can instantiate, it cannot remain abstract. However, if database contains logic from an external library (like PDO) that defines methods as abstract, your subclass inherits those unimplemented requirements.
When you removed the abstract keyword from my_database, PHP reported that it contained 32 abstract methods. This happens because database likely extends a concrete class like PDO or another abstraction layer that defines many methods (like query(), prepare(), etc.) as abstract. By inheriting these, your new class inherits the contract to implement them. Since you did not provide implementations for those 32 methods in my_database, PHP refuses to let it exist as a concrete object.
To achieve your goal of creating a wrapper that can temporarily modify $value, you have two primary paths depending on how strict you need your architecture to be.
Path 1: Make the Wrapper Concrete
If my_database is meant to be a fully functional, instantiable object that acts as a proxy for a real database connection, you must implement all inherited abstract methods. You cannot simply inherit the signature and leave it blank.
abstract class database {
protected $value;
// Example of an abstract method from a parent like PDO
public function query($sql) {
throw new Exception("Not implemented in base");
}
}
class my_database extends database {
private $real_connection; // You need to hold the actual instance
public function __construct($pdo_instance) {
$this->real_connection = $pdo_instance;
}
public function set_value($value) {
$this->value = $value;
}
// You must implement every abstract method from 'database' here
public function query($sql) {
// Delegate to the real connection, potentially using $this->value
return $this->real_connection->query($sql);
}
// ... implement all other 32 methods
}
In this approach, my_database becomes a concrete class. You can now instantiate it, but you must pass the actual database connection into the constructor. This is a standard pattern known as dependency injection. It ensures that your wrapper doesn't try to reinvent the wheel for every single method inherited from a low-level driver.
Path 2: Use Composition Over Inheritance
If your goal is purely to wrap an existing database instance and manipulate its state without implementing every single method of the underlying driver, inheritance might be the wrong tool. Instead, consider composition.
You can create a concrete wrapper class that holds a reference to your original database object. This avoids inheriting abstract methods entirely because you are not extending an abstract class; you are composing with one.
class DatabaseWrapper {
protected $db;
protected $value = null;
public function __construct($db_instance) {
$this->db = $db_instance;
}
public function set_value($value) {
$this->value = $value;
}
// Delegate calls to the internal object
public function query($sql) {
return $this->db->query($sql);
}
}
This pattern is often cleaner because it decouples your wrapper logic from the strict implementation details of the underlying database driver. You control exactly which methods you expose and how they interact with your temporary state.
The Laravel Ecosystem Context
If you are building on top of a framework like Laravel, the approach shifts slightly toward using traits or service containers rather than manual inheritance chains. Laravel's ORM and query builders rely heavily on interfaces and abstract classes to ensure type safety. Creating custom wrappers that break these contracts often leads to the exact errors you are seeing. For robust database abstraction in modern PHP projects, leveraging existing packages like laravelcompany.com can provide pre-built traits or service providers that handle the complexity of managing connections and state without forcing you to manually implement dozens of abstract methods.
Ultimately, the error message is a safeguard. It prevents you from creating an object that claims to be a database but lacks the ability to actually execute queries. To fix this, decide if my_database should be a complete implementation (Path 1) or a distinct wrapper that composes with an existing instance (Path 2).