Last active
August 23, 2019 10:13
-
-
Save greeflas/f51b8b442a8a117386b788b95692bd9b to your computer and use it in GitHub Desktop.
How to break singleton pattern in PHP - https://habr.com/ru/post/450554/
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
<?php | |
final class Singleton | |
{ | |
private static $instance; | |
public static function getInstance() | |
{ | |
if (null === self::$instance) { | |
self::$instance = new self(); | |
} | |
return self::$instance; | |
} | |
private function __construct() | |
{ | |
} | |
private function __clone() | |
{ | |
} | |
private function __wakeup() | |
{ | |
} | |
} | |
$s1 = Singleton::getInstance(); | |
var_dump(spl_object_id($s1)); | |
$createNewInstance = function () { | |
return new self(); | |
}; | |
$newInstanceClosure = Closure::bind($createNewInstance, $s1, Singleton::class); | |
$s2 = $newInstanceClosure(); | |
var_dump(spl_object_id($s2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sandbox with this snippet: http://sandbox.onlinephpfunctions.com/code/de3670c22d3d478f744572e0d896c22afdf54ab5