Skip to content

Commit 9b5e489

Browse files
fixing-database-instance-singleton
1 parent 7f5aad9 commit 9b5e489

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

src/Application/DatabaseConnection.php

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
final class DatabaseConnection
1010
{
11+
private static ?self $instance = null;
12+
1113
private readonly string $id;
1214
private readonly string $host;
1315
private readonly int $port;
@@ -17,7 +19,7 @@ final class DatabaseConnection
1719
private readonly string $password;
1820

1921
private ?PDO $pdo = null;
20-
private readonly \PDOStatement|bool $statement;
22+
private \PDOStatement|bool $statement;
2123

2224
public function __construct(array $config)
2325
{
@@ -30,10 +32,33 @@ public function __construct(array $config)
3032

3133
// Generate a unique ID for each connection
3234
$this->id = uniqid(uniqid('conn_', true));
35+
dump("connection id: {$this->id}");
3336

3437
$this->connect();
3538
}
3639

40+
/**
41+
* @details Private __clone to prevent cloning
42+
*
43+
* @return void
44+
*/
45+
private function __clone(): void
46+
{
47+
}
48+
49+
/**
50+
* @param array $config
51+
* @return self
52+
*/
53+
public static function getInstance(array $config): self
54+
{
55+
if (self::$instance === null) {
56+
self::$instance = new self($config);
57+
}
58+
59+
return self::$instance;
60+
}
61+
3762
/**
3863
* @return \PDO;
3964
*/
@@ -71,13 +96,13 @@ public function query(string $sql): void
7196
}
7297

7398
/**
74-
* @param $parameter
75-
* @param $value
76-
* @param $type
99+
* @param int|string $parameter
100+
* @param mixed $value
101+
* @param ?int $type
77102
*
78103
* @return void
79104
*/
80-
public function bind($parameter, $value, $type = null): void
105+
public function bind(int|string $parameter, mixed $value, ?int $type = null): void
81106
{
82107
switch (is_null($type)) {
83108
case is_int($value):
@@ -99,7 +124,7 @@ public function bind($parameter, $value, $type = null): void
99124
/**
100125
* @return bool
101126
*/
102-
public function execute()
127+
public function execute(): bool
103128
{
104129
return $this->statement->execute();
105130
}
@@ -109,26 +134,28 @@ public function execute()
109134
*/
110135
public function resultSet(): array
111136
{
112-
$this->execute();
113-
114137
return $this->statement->fetchAll(PDO::FETCH_OBJ);
115138
}
116139

117-
public function single()
140+
/**
141+
* @return \stdClass
142+
*/
143+
public function single(): \stdClass
118144
{
119-
$this->execute();
120-
121145
return $this->statement->fetch(PDO::FETCH_OBJ);
122146
}
123147

124148
/**
125149
* @return int
126150
*/
127-
public function rowCount()
151+
public function rowCount(): int
128152
{
129153
return $this->statement->rowCount();
130154
}
131155

156+
/**
157+
* @return \PDO
158+
*/
132159
public function getPdo(): PDO
133160
{
134161
return $this->pdo;

0 commit comments

Comments
 (0)