Last active
December 14, 2015 04:49
-
-
Save bastman/5030792 to your computer and use it in GitHub Desktop.
GenericSingleton in PHP
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 | |
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; | |
@radmen thx for your thoughts. You are right.
@radmen I re-implemented the getInstance() method in child classes to solve ide type hinting issues. Thats the only reason. For php itself you are right. There is no need to override that method within child classes.
Btw, same type hinting issue on inheritance in conjunction with chaining/fluid interfaces.
good job
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's no need to overwrite
getInstance()
.get_called_class
will return the class name from which was called (You can check: https://gist.github.com/radmen/5031131 tested on PHP 5.3.x).To be true singleton
A1
should also define__construct()
method with private scope.