开发者

singleton in abstract class php

I have a simple question. I use a singleton which implements an abstract class. Is it possible to put the getInstance() Method and the variable $_instance in the abstract class instead of the concrete one I want to create?

Here's my 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 realSimul开发者_如何学编程ate($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 the commands, so I don't want redundant code everywhere in my implementations. Is it possible to put these two things in the abstract class, if yes please tell me how.

If not please explain it to me why it isnt possbile. Or if I need to change something to do it, anyhow.

regards


YES WE CAN!

I have a class called Singleton that is abstract... and many classes that extends that Singleton class... this is the 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 .' already 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) the class DBConnection that extends from Singleton

class DBConnection extends Singleton{

    private $connexion_pdo=null;

    protected function initialize(){
             //connect to the DB
        $this->connexion_pdo = blablalba;
    }
}

although there are some problems in php 5.2.. specially with the function get_called_class() and the static::initialize()

You could also check the php site for patterns... there are lots of contributions for the singleton

Good Luck

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜