How to use/apply the Register design template? Where do you keep the data?



  • The Internet is full of examples of this class. But I don't write anywhere, but where to keep the data.

    After rebooting the page, all the data we kept in some internal variable will be missing.

    These people know something, but they don't?

    Or this template needs to be used in some way with the vault. memcached?

    Or a vault. memcached Is that what this patter is doing and you just need to use it?

    Is the only thing that can be used for cashing or something? How (if I wrote above - after rebooting the data is erased)?



  • Register design template used not for cashingand mainly to replace global variables because global variables are not very good.

    Roster

    class Registry
    {
    
    /**
     * данные реестра   
     */
    protected static $data = array();
    
    
    /**
     * Добавляет значение в реестр
     *
     */
    public static function set($key, $value)
    {
        self::$data[$key] = $value;
    }
    
    /**
     * Возвращает значение из реестра по ключу
     */
    public static function get($key)
    {
    if(isset(self::$data[$key])) {
        return  self::$data[$key];
    }
    return null;
    }
    
    /**
     * Удаляет значение из реестра по ключу
     *
     */
    public static function removeVar($key)
    {
        if(isset(self::$data[$key])) {
            unset(self::$data[$key]);
        }
    }
    

    }

    But, unlike global variables, using this pathterna, you can add some extra chips, like blocking the variable from change.

    Expand opportunities

    /**

    • Добавим массив с для идентификации залоченных переменых
      */
      protected static $locked = array();

    /**

    • Поменяем метод set из примера выше
      */
      static public function set($key, $value) {
      if ( !self::hasLock($key) ) {
      self::$data[$key] = $value;
      } else {
      throw new Exception("переменная '$key' заблокирована для изменений");
      }
      }

    /**

    • напишем "обвес" для блокировки/разблокировки
      */
      static public function lock($key) {
      self::$lock[$key] = true;
      }
      static public function hasLock($key) {
      return isset(self::$lock[$key]);
      }
      static public function unlock($key) {
      if ( self::hasLock($key) ) {
      unset(self::$lock[$key]);
      }
      }

    And if you need to keep the data from Reestre somewhere, you can record them anywhere.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2