Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Moja pierwsza klasa - rejestr
q.michal
post
Post #1





Grupa: Zarejestrowani
Postów: 111
Pomógł: 1
Dołączył: 24.12.2013

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


Czesc wszystkim,

Poznaje PHP i w ramach nauki napisalem swoja 1 klase do obslugi rejestru. Bede wdzieczny za wszelka krytyke.

  1. final class Registry {
  2. private $name;
  3. private $registry;
  4.  
  5. /**
  6.   * Disables object cloning
  7.   *
  8.   * @return void does not return any value
  9.   */
  10. private function __clone() {
  11. throw new Exception('Cloning Registry object is not allowed!');
  12. }
  13.  
  14. /**
  15.   * Class constructor
  16.   *
  17.   * @param string instance name
  18.   * @return void does not return any value
  19.   */
  20. private function __construct($name) {
  21. $this->name = $name;
  22. $this->registry = array();
  23. }
  24.  
  25. /**
  26.   * Adds an element to the registry
  27.   *
  28.   * @param string location in the registry, in which to store the value
  29.   * @param mixed value to be stored in the registry
  30.   * @param boolean specifies whether the existing element should be overwritten or not
  31.   * @return boolean TRUE on success, FALSE otherwise
  32.   */
  33. public function addValue($index, $value, $overwrite = false) {
  34. if($this->isRegistered($index) && !$overwrite) {
  35. return false;
  36. }
  37. $this->registry[$index] = $value;
  38. return true;
  39. }
  40.  
  41. /**
  42.   * Adds multiple elements to the registry
  43.   *
  44.   * @param array associative array containing elements to add to the registry
  45.   * @param boolean specified whether the existing elements should be overwritten or not
  46.   * @return boolean TRUE on success, FALSE otherwise
  47.   */
  48. public function addValues($values, $overwrite = false) {
  49. if(!is_array($values)) {
  50. return false;
  51. }
  52. foreach($values as $index => $value) {
  53. if(!$this->addValue($index, $value, $overwrite)) {
  54. return false;
  55. }
  56. }
  57. return true;
  58. }
  59.  
  60. /**
  61.   * Flushes (removes all elements from) the registry
  62.   *
  63.   * @return void does not return any value
  64.   */
  65. public function flushRegistry() {
  66. $this->registry = array();
  67. }
  68.  
  69. /**
  70.   * Returns the global Registry object, creating it only if it does not exist already
  71.   *
  72.   * @param string instance name
  73.   * @return object the QRegistry object
  74.   */
  75. public static function getInstance($name = 'main') {
  76. static $instance;
  77. if(!isset($instance[$name])) {
  78. $instance[$name] = new Registry();
  79. }
  80. return $instance[$name];
  81. }
  82.  
  83. /**
  84.   * Returns all elements stored in registry
  85.   *
  86.   * @return array associative array containing all registered values
  87.   */
  88. public function getRegistry() {
  89. return $this->registry;
  90. }
  91.  
  92. /**
  93.   * Returns the Registry instance name
  94.   *
  95.   * @return string instance name
  96.   */
  97. public function getRegistryName() {
  98. return $this->name;
  99. }
  100.  
  101. /**
  102.   * Retrieves a value from the registry
  103.   *
  104.   * @param string location in the registry, in which the value is stored
  105.   * @param mixed the default value to return when element is not found
  106.   * @return mixed the value of requested item
  107.   */
  108. public function getValue($index, $default = NULL) {
  109. if(!$this->isRegistered($index)) {
  110. return $default;
  111. }
  112. return $this->registry[$index];
  113. }
  114.  
  115. /**
  116.   * Retrieves multiple values from the registry
  117.   *
  118.   * @param array array containing the names of the requested elements
  119.   * @param mixed the default value to return when element is not found
  120.   * @return array associative array containing values of the requested items
  121.   */
  122. public function getValues($indexes, $default = NULL) {
  123. $results = array();
  124. if(!is_array($indexes)) {
  125. return $results;
  126. }
  127. foreach($indexes as $index) {
  128. $results[$index] = $this->getValue($index, $default);
  129. }
  130. return $results;
  131. }
  132.  
  133. /**
  134.   * Checks if container contains an item with the specified index
  135.   *
  136.   * @param string location in the registry
  137.   * @return boolean TRUE if index is a named value in the registry, FALSE otherwise
  138.   */
  139. public function isRegistered($index) {
  140. return array_key_exists($index, $this->registry);
  141. }
  142.  
  143. /**
  144.   * Removes given registered element
  145.   *
  146.   * @param string location in the registry
  147.   * @return boolean TRUE on success, FALSE otherwise
  148.   */
  149. public function removeValue($index) {
  150. if(!$this->isRegistered($index)) {
  151. return false;
  152. }
  153. unset($this->registry[$index]);
  154. return true;
  155. }
  156.  
  157. /**
  158.   * Removes multiple given registered elements
  159.   *
  160.   * @param array array containing the names of the requested elements
  161.   * @return boolean TRUE on success, FALSE otherwise
  162.   */
  163. public function removeValues($indexes) {
  164. if(!is_array($indexes)) {
  165. return false;
  166. }
  167. foreach($indexes as $index) {
  168. if(!$this->removeValue($index)) {
  169. return false;
  170. }
  171. }
  172. return true;
  173. }
  174.  
  175. }


Ten post edytował q.michal 14.02.2016, 18:06:41
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
com
post
Post #2





Grupa: Zarejestrowani
Postów: 3 034
Pomógł: 366
Dołączył: 24.05.2012

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


Fred1485 owszem

q.michal poczytaj o registry pattern i napisz go jeszcze raz tu masz przykład http://www.phpbar.de/w/Registry
Go to the top of the page
+Quote Post

Posty w temacie
- q.michal   Moja pierwsza klasa - rejestr   14.02.2016, 12:52:24
- - Pyton_000   - Konstruktor bez sensu. - getInstance referencja ...   14.02.2016, 13:13:11
|- - q.michal   Cytat(Pyton_000 @ 14.02.2016, 13:13:1...   14.02.2016, 13:36:29
- - com   CytatCytat(Pyton_000 @ 14.02.2016, 13:13:11 ) * - ...   14.02.2016, 14:04:07
- - q.michal   To wszystko prawda, jednak nadal nie rozumiem co j...   14.02.2016, 14:10:44
- - com   bo w singletonie chodzi przecież o to żeby była 1 ...   14.02.2016, 14:17:51
|- - q.michal   Cytat(com @ 14.02.2016, 14:17:51 ) bo...   14.02.2016, 14:32:35
- - com   no wiec nie rozumiesz singletona, po to stworzyłeś...   14.02.2016, 14:49:14
- - q.michal   Inaczej. Zgadzam sie, ze nie da sie uzyc operator...   14.02.2016, 14:55:44
- - com   nie ma różnicy, ale w konstruktorze wypełnia się j...   14.02.2016, 14:58:49
- - q.michal   Jezeli konstruktora nie wywoluje, to: [PHP] pobie...   14.02.2016, 15:08:39
- - com   nie wywołujesz go jawnie, wiec ustawianie w nim cz...   14.02.2016, 15:17:07
- - Fred1485   Jeśli już o singletonie mówiąc to nie powinno się ...   14.02.2016, 15:26:50
- - com   Fred1485 owszem q.michal poczytaj o registry patt...   14.02.2016, 15:32:54
- - q.michal   Ale chodzi tylko o ten kontrowersyjny konstruktor?...   14.02.2016, 18:04:30
- - com   no poco tam wepchałeś singleton, który stracił sen...   14.02.2016, 18:52:47
- - q.michal   Ale rozumiesz, ze getInstance zwraca zawsze ta sam...   14.02.2016, 19:18:56
- - com   Ale na tym polega singleton, jeśli używasz wzorca,...   14.02.2016, 19:28:17
- - Fred1485   Może jakbyś zrobił abstrakcyjną klasę Registry, a ...   14.02.2016, 19:34:08


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

 



RSS Aktualny czas: 16.02.2026 - 18:41