Reply to comment

  • warning: array_fill(): Number of elements must be positive in /var/www/vhosts/benjaminradtke.com/httpdocs/includes/database.inc on line 253.
  • warning: implode(): Invalid arguments passed in /var/www/vhosts/benjaminradtke.com/httpdocs/includes/database.inc on line 253.
  • warning: array_keys() expects parameter 1 to be array, null given in /var/www/vhosts/benjaminradtke.com/httpdocs/modules/user/user.module on line 528.
  • user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 query: SELECT p.perm FROM role r INNER JOIN permission p ON p.rid = r.rid WHERE r.rid IN () in /var/www/vhosts/benjaminradtke.com/httpdocs/modules/user/user.module on line 528.

Registry Pattern

No votes yet

Das Registry Pattern bietet trotz seiner Einfachheit ein komfortables klassenübergreifendes Daten Handling. Er ermöglicht der Applikation Daten abzulegen um diese an völlig anderer Stelle wieder zu erreichen ohne diese aufwendig "mitzuschleifen".

  1. <?php
  2.  
  3. class Registry {
  4. private static $_storage = array();
  5.  
  6. public static function add($key, &$value) {
  7. if (self::exists($key)) {
  8. throw new Exception('Registry key "'.$key.'" already exists!');
  9. }
  10. self::$_storage[$key] = $value;
  11. }
  12.  
  13. public static function & get($key) {
  14. if (array_key_exists($key, self::$_storage) {
  15. return self::$_storage[$key];
  16. }
  17. throw new Exception('Registry key "'.$key.'" not exists!');
  18. }
  19.  
  20. public static function exists($key) {
  21. return isset(self::$_storage[$key]);
  22. }
  23. }

Die Klasse Registry bietet statische Methoden um Werte unter einem Schlüssel in der statischen Eigenschaft $_storage abzulegen und auszulesen.

Dadurch ergibt sich folgende Verwendung:

  1. <?php
  2.  
  3. $foo = new Foo(); // irgendein Objekt
  4. $bar = 512;
  5.  
  6. // ablegen der Daten
  7. Registry::add('foo', $foo);
  8. Registry::add('bar', $bar);
  9. ...
  10.  
  11. // völlig andere Stelle innerhalb der Applikation
  12. $foo = Registry::get('foo');
  13. $bar = Registry::get('bar');

Es sei jedoch darauf hingewiesen, dass man nicht versuchen sollte, schlechte Objektorientierung bzw. schlechtes Anwendungsdesign durch den übertriebenen Einsatz des Registry Patterns auszugleichen.

Reply

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <abbr>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Tags