Singleton

85
публикации

Можно передавать параметры при первом вызове getInstance у singleton

abstract class Singleton implements \Serializable
{
    private static $instances = array();
    
    protected function __construct() {}
    
    public function __clone() {
        $msg = 'Clone is not allowed.';
        throw new Exception($msg);
    }
    
    /**
     * @param string $classname
     * @return self
     */
    protected static function getInstanceOf($classname, $parameters) {
        if(!isset(self::$instances[$classname])) {
            self::$instances[$classname] = new $classname($parameters);
        }
        return self::$instances[$classname];
    }
    
    public static function instanceExists($classname = null) {
        return array_key_exists($classname ?: get_called_class(), self::$instances);
    }
    
    /**
     * @param void
     * @return self
     */
    public static function getInstance() {
        return self::getInstanceOf(get_called_class(), func_get_args());
    }
    
    /**
     * @param void
     * @return void
     * @throws \BadFunctionCallException
     */
    public function serialize() {
        throw new \BadFunctionCallException('You cannot serialize this object.');
    }
    
    /**
     * @param void
     * @return void
     * @throws \BadFunctionCallException
     */
    public function unserialize($serialized) {
        throw new \BadFunctionCallException('You cannot unserialize this object.');
    }
}

Tags
#archive 62 #capsulecms 30 #featurerequest 26 #linux 22 #php 13 #windows 12 #phpstorm 11 #eclipse 8 #font 6 #nginx 5 #bug 5 #oldsite 4 #iosevka 3 #SEO 3 #mvc 2 #nano 2 #antipattern 2 #quotes 2 #cmd 1 #iconv 1 #cms 1 #skype 1 #pdf 1 #algorithm 1 #composer 1 #copypaste 1 #sourcetree 1 #singleton 1 #mysql 1 #meld 1 #pattern 1 #postgresql 1 #debian 1 #ideas 1 #strtok 1 #qt 1 #capsulecmsv1 1 #netbeans 1 #puntoswitcher 1 #movie 1 #mariadb 1 #foobar 1 #apache 1 #git 1 #pidgin 1 #curl 1 #datetimepicker 1 #rsync 1 #bootstrap 1