forked from gabrielanhaia/php-design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0101672
commit c1f3027
Showing
4 changed files
with
55 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
use App\{Singleton}; | ||
|
||
require_once 'autoloader.php'; | ||
|
||
$dbConnectionInstance1 = Singleton\DBConnectionSingleton::getConnection(); | ||
$dbConnectionInstance2 = Singleton\DBConnectionSingleton::getConnection(); | ||
|
||
echo $dbConnectionInstance1->doSomething(); | ||
ln(); | ||
echo $dbConnectionInstance2->doSomething(); | ||
ln(); | ||
|
||
if (spl_object_id($dbConnectionInstance1) === spl_object_id($dbConnectionInstance2)) { | ||
dump('Object "$dbConnectionInstance1" and "$dbConnectionInstance2" are exactly the same in memory.'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Singleton; | ||
|
||
class DBConnectionSingleton | ||
{ | ||
private static ?DBConnectionSingleton $instance = null; | ||
|
||
private function __construct() | ||
{ | ||
} | ||
|
||
private function __clone(): void | ||
{ | ||
} | ||
|
||
public static function getConnection(): self | ||
{ | ||
if (self::$instance === null) { | ||
self::$instance = new self(); | ||
} | ||
|
||
return self::$instance; | ||
} | ||
|
||
public function doSomething(): string | ||
{ | ||
return 'It worked!'; | ||
} | ||
} |