Registry

Aus php bar
Wechseln zu: Navigation, Suche

Implementierung

Um aus verschiedenen Klassen auf "globale" Objekte und Variablen zuzugreifen, gibt es das Registry-Entwurfsmuster. Die Registry kann als abstract deklariert werden, da nur statische Attribute und Methoden verwendet werden. virtual assistant

1   <?php
2   /**
3    * Registry class to pass global variables between classes.
4    */
5   abstract class Registry
6   {
7       /**
8        * Object registry provides storage for shared objects
9        *
10       * @var array 
11       */
12      private static $registry = array();
13      
14      /**
15       * Adds a new variable to the Registry.
16       *
17       * @param string $key Name of the variable
18       * @param mixed $value Value of the variable
19       * @throws Exception
20       * @return bool 
21       */
22      public static function set($key, $value) {
23          if ( ! self::has($key) ) {
24              self::$registry[$key] = $value;
25              return true;
26          } else {
27              throw new Exception('Unable to set variable `' . $key . '`. It was already set.');
28          }
29      }
30  
31      /**
32       * Tests if given $key exists in registry
33       *
34       * @param string $key
35       * @return bool
36       */
37      public static function has($key)
38      {
39          if ( isset( self::$registry[$key] ) ) {
40              return true;
41          }
42  
43          return false;
44      }
45   
46      /**
47       * Returns the value of the specified $key in the Registry.
48       *
49       * @param string $key Name of the variable
50       * @return mixed Value of the specified $key
51       */
52      public static function get($key)
53      {
54          if ( self::has($key) ) {
55              return self::$registry[$key];
56          }
57          return null;
58      }
59   
60      /**
61       * Returns the whole Registry as an array.
62       *
63       * @return array Whole Registry
64       */
65      public static function getAll()
66      {
67          return self::$registry;
68      }
69   
70      /**
71       * Removes a variable from the Registry.
72       *
73       * @param string $key Name of the variable
74       * @return bool
75       */
76      public static function remove($key)
77      {
78          if ( self::has($key) ) {
79              unset(self::$registry[$key]);
80              return true;
81          }
82          return false;
83      }
84   
85      /**
86       * Removes all variables from the Registry.
87       *
88       * @return void 
89       */
90      public static function removeAll()
91      {
92          self::$registry = array();
93          return;
94      }
95  }
96  
97  // Anwendung
98  Registry::set('test', 'Ich bin ueberall verfuegbar.');
99  echo Registry::get('test'); // Output: ich bin ueberall verfuegbar.
100 ?>


Um Codevervollständigung zu unterstützen muss man die Implementierung hier noch etwas abändern. Hier ein Beispiel, wo die Variable $registry immer global aufgerufen wird:

 <?php
 /**
  * Registry class to pass global variables between classes.
  * 
  * @property string $test
  */
 class Registry
 {
 	/**
10 	 * Object registry provides storage for shared objects
11 	 *
12 	 * @var array 
13 	 */
14 	private $registry = array();
15 
16 	/**
17 	 * Adds a new variable to the Registry.
18 	 *
19 	 * @param string $key Name of the variable
20 	 * @param mixed $value Value of the variable
21 	 * @return void 
22 	 */
23 	public function __set($key, $value)
24 	{
25 		if ((null === $value) && isset($this->registry[$key])) {
26 			unset($this->registry[$key]);
27 		} elseif (null !== $value) {
28 			$this->registry[$key] = $value;
29 		}
30 	}
31 
32 	/**
33 	 * Tests if given $key exists in registry
34 	 *
35 	 * @param string $key
36 	 * @return bool
37 	 */
38 	public function __isset($key)
39 	{
40 		if ( isset( $this->registry[$key] ) ) {
41 			return true;
42 		}
43 
44 		return false;
45 	}
46 
47 	/**
48 	 * Returns the value of the specified $key in the Registry.
49 	 *
50 	 * @param string $key Name of the variable
51 	 * @return mixed Value of the specified $key
52 	 */
53 	public function __get($key)
54 	{
55 		if ( $this->isset($key) ) {
56 			return $this->registry[$key];
57 		}
58 		return null;
59 	}
60 }
61 
62 // Anwendung
63 $registry = new Registry();
64 $registry->test = 'Ich bin ueberall verfuegbar.';
65 echo $registry->test; // Output: ich bin ueberall verfuegbar.
66 ?>