Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
use DependencyInjector\DI;
class DatabaseObject {
/**
* @var PDO
*/
private static $connection;
/**
* Execute a query and return the result set.
*
* @param string $sql
* @return array
* @throws Exception
*/
public function query($sql) {
$connection = self::getConnection();
$result = $connection->query($sql);
if (!$result) {
throw new Exception('SQL error');
}
return $result->fetchAll();
}
/**
* @return PDO
*/
private static function getConnection() {
if (!self::$connection) {
self::$connection = new PDO('mysql:host=localhost;dbname=anything');
}
return self::$connection;
}
}
// has to be added (maybe at config stage or bootstrap)
//DI::set('database', function() { return new DatabaseObject(); }, false);