-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDBConnProvider.php
More file actions
85 lines (58 loc) · 1.77 KB
/
DBConnProvider.php
File metadata and controls
85 lines (58 loc) · 1.77 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
class DBConnProvider{
private static $instance = null;
private $conn;
private $host = 'localhost';
private $user = 'root';
private $pass = '';
private $dbName = 'performance_schema';
private $driver = 'mysql';
private function __construct() {
echo 'Kurucu çalıştı.<br>';
$this -> conn = new PDO(
"{$this->driver}:host={$this->host};dbname={$this->dbName}",
$this -> user,
$this -> pass
);
}
public static function getInstance() {
if (!self::$instance){
self::$instance = new DBConnProvider();
echo 'yeni DBConnProvider nesnesi türetildi.<br>';
}else
echo 'DBConnProvider nesnesi ram bellekten getirildi.<br>';
return self::$instance;
}
public function getConnection(){
return $this -> conn;
}
public function getServerInfo(){
return $this->conn->getAttribute(PDO::ATTR_SERVER_INFO);
}
public function getDbName(){
return $this->dbName;
}
public function getDriver(){
return $this->driver;
}
public function getUserName(){
return substr($this->user, 0,2).'....';
}
public function __clone() {
throw new Exception("Bu nesneyi klonlayamazsın.");
}
}
$dbConnProvider = DBConnProvider::getInstance();
$conn = $dbConnProvider->getConnection();
$conn->query();
var_dump($conn);
$dbConnProvider = DBConnProvider::getInstance();
$conn = $dbConnProvider->getConnection();
var_dump($conn);
$dbConnProvider = DBConnProvider::getInstance();
//$tryCloneInstance = clone $dbConnProvider;
$conn = $dbConnProvider->getConnection();
var_dump($conn);
echo 'Veritabanı sunucusu anlık durumu: '.$dbConnProvider->getServerInfo().'<br>';
echo 'Veritabanına '.$dbConnProvider->getUserName().' kullanıcısı ile bağlanıldı.<br>';
echo 'Veritabanı yazılımı : '.$dbConnProvider->getDriver().' <br>';