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 hidden or 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; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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.