Skip to content

Commit c579819

Browse files
committed
PDO Refactoring and Collaborators
1 parent 8c34330 commit c579819

6 files changed

Lines changed: 42 additions & 38 deletions

File tree

Task.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

bootstrap.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
require 'database/Connection.php';
4+
require 'database/QueryBuilder.php';
5+
6+
return new QueryBuilder(
7+
Connection::make()
8+
);

database/Connection.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
class Connection
3+
{
4+
5+
public static function make() {
6+
try {
7+
return new PDO('mysql:host=127.0.0.1;dbname=mytodo', 'prakhar', '');
8+
} catch(PDOException $e) {
9+
die($e->getMessage());
10+
}
11+
}
12+
13+
}

database/QueryBuilder.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
class QueryBuilder
4+
{
5+
protected $pdo;
6+
public function __construct($pdo)
7+
{
8+
$this->pdo = $pdo;
9+
}
10+
11+
public function selectAll($table)
12+
{
13+
$statement = $this->pdo->prepare("select * from {$table}");
14+
15+
$statement->execute();
16+
// Generally little careful about it, fetch
17+
return $statement->fetchAll(PDO::FETCH_CLASS);
18+
}
19+
}

functions.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

index.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
require 'functions.php';
4-
require 'Task.php';
3+
$query = require 'bootstrap.php';
54

6-
$pdo = connectToDb();
7-
$tasks = fetchAllTasks($pdo);
5+
$tasks = $query->selectAll('todos');
86

97
require 'index.view.php';

0 commit comments

Comments
 (0)