Skip to content

Instantly share code, notes, and snippets.

@bastman
Last active December 14, 2015 04:49
Show Gist options
  • Save bastman/5030792 to your computer and use it in GitHub Desktop.
Save bastman/5030792 to your computer and use it in GitHub Desktop.
GenericSingleton in PHP
<?php
class A1 {
/**
* @var array
*/
private static $instanceDictionary = array();
/**
* @return self
*/
public static function getInstance()
{
$calledClass = get_called_class();
$dictionary = self::$instanceDictionary;
if(!array_key_exists($calledClass, $dictionary)) {
$reflectionClass = new \ReflectionClass($calledClass);
$reflectionMethod = $reflectionClass->getMethod('createInstance');
$reflectionMethod->setAccessible(true);
$dictionary[$calledClass] = $reflectionMethod->invokeArgs(
null, func_get_args()
);
self::$instanceDictionary = $dictionary;
}
return $dictionary[$calledClass];
}
/**
* @return self
*/
public static function createInstance()
{
var_dump('create '.get_called_class());
$className = get_called_class();
return new $className();
}
public $x1 = 10;
/**
* thx. to radmen
**/
protected function __construct()
{
}
}
class A2 extends A1 {
/**
* Just to fix type hinting issues. otherwiese my PHPStorm ide assumes it to be A1 type
* @return self
*/
public static function getInstance()
{
return parent::getInstance();
}
public $x2 = 10;
}
class A3 extends A2 {
/**
* Just to fix type hinting issues. otherwiese my PHPStorm ide assumes it to be A1 type
* @return self
*/
public static function getInstance()
{
return parent::getInstance();
}
public $x3 = 10;
public $x1 = 5;
}
$a1 = A1::getInstance();
$a2 = A2::getInstance();
$a3 = A3::getInstance();
$a1 = A1::getInstance();
$a2 = A2::getInstance();
$a3 = A3::getInstance();
var_dump($a1);
var_dump($a2);
var_dump($a3);
exit;
@flyingchen
Copy link

good job

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