Framework Error
Error: unable to create an instance of class 'PDO' because 'PDO' is abstract; it can only be instantiated from a subclass.
What This Error Means
This error occurs when attempting to create an instance of an abstract class, which can't be directly instantiated. In the context of PHP's PDO (PHP Data Objects) framework, this typically happens when a developer tries to use PDO without extending it with a concrete subclass.
Why It Happens
This error happens when a developer tries to use PDO without extending it with a concrete subclass. In PHP, abstract classes can't be directly instantiated. Instead, they need to be extended with a concrete subclass that implements all abstract methods.
How to Fix It
- 1To fix this error, you need to create a subclass of PDO that extends it with concrete implementation. Here's an example:
- 2// Before (broken code)
- 3$pdo = new PDO('dsn', 'username', 'password');
- 4// After (fixed code)
- 5class MyPDO extends PDO {
- 6 public function __construct($dsn, $username, $password) {
- 7 parent::__construct($dsn, $username, $password);
- 8 }
- 9}
- 10$pdo = new MyPDO('dsn', 'username', 'password');
Example Code Solution
$pdo = new PDO('dsn', 'username', 'password');class MyPDO extends PDO {
public function __construct($dsn, $username, $password) {
parent::__construct($dsn, $username, $password);
}
}
$pdo = new MyPDO('dsn', 'username', 'password');Fix for Error: unable to create an instance of class 'PDO' because 'PDO' is abstract; it can only be instantiated from a subclass.
Browse Related Clusters
Related SQL Errors
Related SQL Blog Articles
Have a different error? Get an instant explanation.
Explain Another Error