MemoryRefresh!

Patterns / Registry   Registry



Questions Answers 0%

Pages   0 from 91
Questions   0 from 142

Reset


Registry Pattern allows any object to be used as Singleton, without it being written as Singleton.
<?php class Article_Controller_Front extends MyLibrary_Controller_Action { public function listAction() { // undefined articleModel (__get called) $articles = $this->articleModel->getArticles(); } } class MyLibrary_Controller_Action { private $_register = array(); // Magic method (called for undefined attributes of an class) public function __get($var) { if (strstr($var, 'Model')) { $modelKey = substr($var, 0, -5); // any object is used as Singleton, // without it being written specifically that way if (in_array($modelKey, $this->_register)) { return $this->_register[$modelKey]; // --- Look Here --- // } else { eval('$model = new Models_' . ucfirst($modelKey) . '();'); $this->_register[$modelKey] = $model; return $model; } } } } class Models_Article { public function getArticles() { echo 'articles'; } } $test = new Article_Controller_Front(); $test->listAction(); // Output: articles

Related Pages

Observer
Singleton
Front
Factory
Registry
Mvc
Active record


0% 100%
 
 
0 pages 91 pages
1) Registry Pattern








Registry Pattern allows any object to be used as Singleton, without it being written as Singleton.
<?php class Article_Controller_Front extends MyLibrary_Controller_Action { public function listAction() { // undefined articleModel (__get called) $articles = $this->articleModel->getArticles(); } } class MyLibrary_Controller_Action { private $_register = array(); // Magic method (called for undefined attributes of an class) public function __get($var) { if (strstr($var, 'Model')) { $modelKey = substr($var, 0, -5); // any object is used as Singleton, // without it being written specifically that way if (in_array($modelKey, $this->_register)) { return $this->_register[$modelKey]; // --- Look Here --- // } else { eval('$model = new Models_' . ucfirst($modelKey) . '();'); $this->_register[$modelKey] = $model; return $model; } } } } class Models_Article { public function getArticles() { echo 'articles'; } } $test = new Article_Controller_Front(); $test->listAction(); // Output: articles

References









Programming

Php
   
Regex
   
MySQL
   
Css
   
jQuery
   
Git
   


References