Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Rejestr do oceny
Black-Berry
post
Post #1





Grupa: Zarejestrowani
Postów: 663
Pomógł: 6
Dołączył: 3.06.2007
Skąd: Kraków

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


*Plik Bootstrap* Każdemu obiektowi który ma mieć zakres globalny przekazujemy w konstruktorze context
  1. <?php
  2. $context = new Context();
  3.    $config = new Config($context);
  4.    $log = new Log($context);
  5.    $dbDriver = new Db_Driver($context);
  6.    $session = new Session($context);
  7.    $language = new Language($context);
  8.    $user = new User($context);
  9.    $structure = new Structure($context);
  10.    $router = new Router($context);
  11.    $buffer = new Buffer($context);
  12. ?>


Każdy obiekt globalny dziedziczy po klasie System_Object
  1. <?php
  2.    class System_Object
  3.    {
  4.        public $context = array();
  5.        
  6.        public function __construct(Context $context)
  7.        {
  8.            $this->context = $context->getRegistry();
  9.            $context->register($this);
  10.        }
  11.        
  12.        public function __get($key)
  13.        {
  14.            if(isset($this->context[$key])) {
  15.                return $this->context[$key];
  16.            }
  17.        }
  18.    }
  19. ?>


Obiekt Kontext
  1. <?php
  2.    class Context
  3.    {
  4.        private $registry = array();
  5.    
  6.        public function __get($key)
  7.        {
  8.            if(isset($this->registry[$key])) {
  9.                return $this->registry[$key];
  10.            }
  11.        }
  12.        
  13.        public function register(System_Object $object)
  14.        {
  15.            $key = System_Strings::variableNotation(get_class($object));
  16.            $this->registry[$key] = $object;
  17.        }
  18.        
  19.        public function getRegistry()
  20.        {
  21.            return $this->registry;
  22.        }
  23.    }
  24.    
  25. ?>


Przykłądowa klasa. Wszystkie inne na podobnej zasadzie. Nie ma żadnych setterów, getterów, wszystko bez zbędnych kodów.
  1. <?php
  2.    class Config extends System_Object
  3.    {
  4.        public $test = 'Wartość testowa';
  5.    
  6.        public function __construct(Context $context)
  7.        {
  8.            parent::__construct($context);
  9.        }
  10.    }  
  11. ?>


sposób użycia
  1. <?php
  2.    print $user->config->test;  // jak widac obiekt wyswietla zmienna z innego obiektu
  3.    $log->config->test = 'foo'; // a zupelnie inny obiekt modyfikuje ta wartosc
  4.    print $user->config->test; // no i wartosc ulega zmienie tak jakby byla globalna
  5. ?>


Moim zdaniem to musi być ostateczne rozwiązanie problemu globalsów. Bardzo proszę o dyskusję

Czy singletony zamiast tego byłyby wydajniejsze ? Podobno singletony to zło. Ja już zgupłem do reszty @_@

Ten post edytował Black-Berry 5.11.2008, 09:24:28


--------------------
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 21.08.2025 - 22:48