Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Coś jak Zend_Registry
Eagle
post
Post #1





Grupa: Zarejestrowani
Postów: 170
Pomógł: 14
Dołączył: 16.03.2007

Ostrzeżenie: (0%)
-----


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.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 5)
AxZx
post
Post #2





Grupa: Zarejestrowani
Postów: 1 385
Pomógł: 55
Dołączył: 1.03.2005
Skąd: śląsk

Ostrzeżenie: (0%)
-----


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. ?>
Go to the top of the page
+Quote Post
Eagle
post
Post #3





Grupa: Zarejestrowani
Postów: 170
Pomógł: 14
Dołączył: 16.03.2007

Ostrzeżenie: (0%)
-----


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. ?>
Go to the top of the page
+Quote Post
AxZx
post
Post #4





Grupa: Zarejestrowani
Postów: 1 385
Pomógł: 55
Dołączył: 1.03.2005
Skąd: śląsk

Ostrzeżenie: (0%)
-----


nie wiem czy poprawiłeś tą linijkę:
  1. <?php
  2. self::$instance = new Config_site;
  3. ?>
Go to the top of the page
+Quote Post
Eagle
post
Post #5





Grupa: Zarejestrowani
Postów: 170
Pomógł: 14
Dołączył: 16.03.2007

Ostrzeżenie: (0%)
-----


Zamieniłem tą linijkę na:
  1. <?php
  2. self::$instance = new self();
  3. ?>
Go to the top of the page
+Quote Post
nexis
post
Post #6





Grupa: Zarejestrowani
Postów: 1 012
Pomógł: 109
Dołączył: 26.09.2003
Skąd: nexis.pl

Ostrzeżenie: (0%)
-----


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. ?>


Ten post edytował nexis 24.12.2008, 01:25:17
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 11:13