-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingletonConnection.php
More file actions
34 lines (28 loc) · 873 Bytes
/
SingletonConnection.php
File metadata and controls
34 lines (28 loc) · 873 Bytes
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
<?php
class SingletonConnection {
private static $instance;
private function __construct() { }
private static function getInstance() {
if (null == SingletonConnection::$instance) {
$cname = __CLASS__;
SingletonConnection::$instance = new $cname;
}
return SingletonConnection::$instance;
}
/**
* @throws Exception when trying to create a copy.
*/
public function __clone(): void
{
throw new Exception('Unable to create copy of object.');
}
public static function connection(): PDO {
try {
return new PDO('mysql:host=localhost;dbname=autoshop', 'root', '');
} catch (PDOException $ex) {
// avoiding to exception swallowing.
echo 'Unable to get connection with database';
throw new $ex;
}
}
}