design patterns - singleton in abstract class php -


i have simple question. use singleton implements abstract class. possible put getinstance() method , variable $_instance in abstract class instead of concrete 1 want create?

here's code:

<?php class command_log extends command_abstract {     private static $_instance=null;      public static function getinstance() {         if (self::$_instance==null)         self::$_instance=new self();         return self::$_instance;     }      protected function realexecute() {     }      protected function realsimulate($filehandle) {     }  } 

and

<?php  abstract class command_abstract implements command_interface {     protected $_data=array();     //private static $_instance=null;     protected $_isexecuted=false;     protected $_execute=false;       public function enableexecute() {         $this->_execute=true;         return $this;     }      protected function __construct() {      }     protected function __clone() {}       public function adddata($data) {          array_push($this->_data,$data);         return $this;     }      abstract protected function realexecute();      abstract protected function realsimulate($filehandle);      public function execute() {         if(!$this->_isexecuted && $this->_execute) {             $this->_isexecuted = true;             $this->realexecute();         }     }      public function simulate() {         $exitsystem = false;         if(!$this->_isexecuted && $this->_execute) {             $this->_isexecuted = true;                 $exitsystem = $this->realsimulate($fh);             }         }         return $exitsystem;     }  } 

i have many implementations of the commands, don't want redundant code everywhere in implementations. possible put these 2 things in abstract class, if yes please tell me how.

if not please explain me why isnt possbile. or if need change it, anyhow.

regards

yes can!

i have class called singleton abstract... , many classes extends singleton class... code:

abstract class singleton {      private static $instances = array();      final private function __construct($_params) {         $class = get_called_class();         if (array_key_exists($class, self::$instances))             throw new exception('an instance of '. $class .' exists !');          //static::initialize(); //in php 5.3         $this->initialize($_params);      }     final private function __clone() { }      abstract protected function initialize();      public static function getinstance($_params=array()) {         $class = get_called_class();         if (array_key_exists($class, self::$instances) === false){             self::$instances[$class] = new $class($_params);         }          return self::$instances[$class];     } } 

and (for example) class dbconnection extends singleton

class dbconnection extends singleton{      private $connexion_pdo=null;      protected function initialize(){              //connect db         $this->connexion_pdo = blablalba;     } } 

although there problems in php 5.2.. specially function get_called_class() , static::initialize()

you check php site patterns... there lots of contributions singleton

good luck


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -