I've got a little problem. Here it is:
This is my singleton abstract class:
abstract class Singleton {
protected static $_instance = NULL;
/**
* Prevent direct object creation
*/
final private function __construct()
{
$this->actionBeforeInstantiate();
}
/**
* Prevent object cloning
*/
final private function __clone() { }
/**
* Returns new or existing Singleton instance
* @return Singleton
*/
final public static function getInstance(){
if(null !== static::$_instance){
return static::$_instance;
}
static::$_instance = new static();
return static::$_instance;
}
abstract protected function actionBeforeInstantiate();
}
After that I create an abstract registry class:
abstract class BaseRegistry extends Singleton
{
//...
}
Now it's time for session registry.
class BaseSessionRegistry extends BaseRegistry
{
//...
protected function actionBeforeInstantiate()
{
session_start();
}
}
The final step:
class AppBaseSessionRegistryTwo extends BaseSessionRegistry { //... }
class AppBaseSessionRegistry extends BaseSessionRegistry { //... }
Testing
$registry = AppBaseSessionRegistry::getInstance();
$registry2 =AppBaseSessionRegistryTwo::getInstance();
echo get_class($registry) . '|' . get_class($registry2) . '<br>';
Output:
AppBaseSessionRegistry|AppBaseSessionRegistry
My expectations were:
AppBaseSessionRegistry|AppBaseSessionRegistryTwo
Why did I get such result? And how can I remake my code to obtain result that I expected?
UPDATE: i use this in my framework. And users will extend my BaseSessionRegistry
class and will add their stuff. I want to solve this inside my framework classes
You need to do this:
class AppBaseSessionRegistryTwo extends BaseSessionRegistry {
protected static $_instance = NULL;
// ...
}
class AppBaseSessionRegistry extends BaseSessionRegistry {
protected static $_instance = NULL;
//...
}
If you don't declare static property separately, they will share the same static property of their parent.
Update: If you don't want the children declare the static property, then you could declare the static property as an array of the parent.
abstract class Singleton {
protected static $_instances = array();
// ...
/**
* Returns new or existing Singleton instance
* @return Singleton
*/
final public static function getInstance(){
$class_name = get_called_class();
if(isset(self::$_instances[$class_name])){
return self::$_instances[$class_name];
}
return self::$_instances[$class_name] = new static();
}
abstract protected function actionBeforeInstantiate();
}
Your first line:
$registry = AppBaseSessionRegistry::getInstance();
will fill the protected static $_instance
with a AppBaseSessionRegistry
instance. On your next line $registry2 =AppBaseSessionRegistryTwo::getInstance();
the protected static $_instance has already a value, and that instance will be returned.
Generally speaking, Singletons and statics are evil. Combining them is horrible. Get in the business of good OO programming and avoid Singletons and statics.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With