Skip to content

Instantly share code, notes, and snippets.

@greeflas
Last active August 23, 2019 10:13
Show Gist options
  • Save greeflas/f51b8b442a8a117386b788b95692bd9b to your computer and use it in GitHub Desktop.
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/
<?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));
@greeflas
Copy link
Author

greeflas commented May 1, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment