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:
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.