Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Coś jak Zend_Registry
Forum PHP.pl > Forum > Przedszkole
Eagle
Witam!

Potrzebuje napisać prostą stronę używając MVC. Po modyfikacjach (http://blog.maxindelicato.com/2008/11/simple-mvc-php-framework.html tego szkieletu) dotarłem do pewnego problemu... Jak uzyskać podobny efekt do Zend_Registry w ZF?

Chodzi mi o to że jak zapisze sobie do rejestru w index.php [ rejestr::set('cos','dana'); ]
to w kontrolerze bez problemu będę mógł go uzyskać [ $dana = rejestr::get('cos'); ]

Przeglądałem kod od ZF ale po kilku godzinach pogubiłem się ...

Jakieś propozycje ?

Pozdrawiam.
AxZx
to nie jest szczególnie trudne zadanie

  1. <?php
  2. class Config_Site_Core{
  3.    
  4.    // The singleton instance of the controller
  5.    public static $instance;
  6.  
  7.    public static function instance(){        
  8.        if (self::$instance === NULL)
  9.        {
  10.            self::$instance = new Config_site;
  11.        }
  12.        return self::$instance;
  13.    }
  14.    
  15.    function __construct($site = NULL){
  16.        if( ! empty($site)){
  17.            $this->site = $site;
  18.        }
  19.    }
  20.    
  21.    function __get($key){
  22.        return ( ! empty($this->$key)) ? $this->$key : NULL;
  23.    }
  24.    
  25.    function __set($key, $value){
  26.        $this->$key = $value;
  27.    }
  28.    
  29.    function set($key, $value){
  30.        $this->$key = $value;
  31.    }
  32.    
  33.    function get($key){
  34.        return ( ! empty($this->$key)) ? $this->$key : NULL;
  35.    }
  36.    
  37.  
  38. }
  39. ?>
Eagle
Wywaliło błąd
Kod
Fatal error: Using $this when not in object context ... on line 35


Przy użyciu
  1. <?php
  2. Config_Site_Core::set('test','abc');
  3. echo Config_Site_Core::get('test');
  4. ?>
AxZx
nie wiem czy poprawiłeś tą linijkę:
  1. <?php
  2. self::$instance = new Config_site;
  3. ?>
Eagle
Zamieniłem tą linijkę na:
  1. <?php
  2. self::$instance = new self();
  3. ?>
nexis
Proponuję taką wersję:

  1. <?php
  2. class Registry
  3. {
  4.   private static $memory;
  5.  
  6.   public static function set($key, $value)
  7.   {
  8.       self::$memory[$key] = $value;
  9.   }
  10.  
  11.   public static function get($key)
  12.   {
  13.       return (isset(self::$memory[$key])) ? self::$memory[$key] : null;
  14.   }
  15.  
  16.  
  17. }
  18. Registry::set('test','abc');
  19. echo Registry::get('test');
  20. ?>


a na Singletonie taką:

  1. <?php
  2. class Registry
  3. {  
  4.   private static $instance;
  5.   private static $memory;
  6.  
  7.   private function __construct()
  8.   {
  9.      // Blokuje publiczny konstruktor
  10.   }
  11.   private function __clone()
  12.   {
  13.      // Blokuje tworzenie kopii obiektu
  14.   }
  15.  
  16.   public static function getInstance()
  17.   {        
  18.       if (self::$instance === NULL)
  19.       {
  20.           self::$instance = new self;
  21.       }
  22.       return self::$instance;
  23.   }
  24.  
  25.   public static function set($key, $value)
  26.   {
  27.       self::$memory[$key] = $value;
  28.   }
  29.  
  30.   public static function get($key)
  31.   {
  32.       return (isset(self::$memory[$key])) ? self::$memory[$key] : null;
  33.   }
  34.  
  35.  
  36. }
  37. $registry = Registry::getInstance();
  38. $registry::set('test','abc');
  39. echo $registry::get('test');
  40. ?>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.