Last active
March 8, 2019 22:21
-
-
Save kanian/f410453456bac9002aeed4b25f50155e to your computer and use it in GitHub Desktop.
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 | |
function singletonize(\Closure $func) | |
{ | |
$singled = new class($func) | |
{ | |
// Hold the class instance. | |
private static $instance = null; | |
public function __construct($func = null) | |
{ | |
if (self::$instance === null) { | |
self::$instance = $func(); | |
} | |
return self::$instance; | |
} | |
// The singleton decorates the class returned by the closure | |
public function __call($method, $args) | |
{ | |
return call_user_func_array([self::$instance, $method], $args); | |
} | |
private function __clone(){} | |
private function __wakeup(){} | |
}; | |
return $singled; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment