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
nexis
post
Post #2





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

Posty w temacie


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: 15.10.2025 - 15:56