OOP with Database — a practical guide to OOP database PHP with clear examples you can reuse in real projects.
OOP Tutorial Series (57/65). Prefer one article? Read the complete OOP tutorial.
Short description
Use connection services, repositories/models, and entities/DTOs to keep SQL organized.
DB wrapper sketch
class Database {
public function __construct(private PDO $pdo) {}
public function fetchAll(string $sql, array $params = []): array {
$stmt = $this->pdo->prepare($sql);
$stmt->execute($params);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}