Ver Mensaje Individual
  #5 (permalink)  
Antiguo 13/12/2009, 19:37
Avatar de destor77
destor77
 
Fecha de Ingreso: noviembre-2004
Ubicación: Gálvez, Santa Fe, Argentina
Mensajes: 2.654
Antigüedad: 19 años, 6 meses
Puntos: 43
Respuesta: duda singleton

navegando por www.phpclasses.org en el grupo de diseño de patrones, encontre un ejemplo de registry en un framework que es asi:
Código PHP:
Ver original
  1. class registry
  2.     {
  3.         # vars that will be available globally
  4.          private $vars = array();
  5.        
  6.          public function __set($index, $value)
  7.          {
  8.             $this->vars[$index] = $value;
  9.          }
  10.        
  11.          public function __get($index)
  12.          {
  13.             return $this->vars[$index];
  14.          }
  15.     }

Es tan simple como eso?

después lo crea en el index.php de la aplicacion
Código PHP:
Ver original
  1. # include the config file
  2.     require 'config.php';
  3.  
  4.     # include the core files
  5.     require __SITE_PATH . '/core/' . 'controller.class.php';
  6.     require __SITE_PATH . '/core/' . 'registry.class.php';
  7.     require __SITE_PATH . '/core/' . 'router.class.php';
  8.     require __SITE_PATH . '/core/' . 'template.class.php';
  9.     require __SITE_PATH . '/core/' . 'model.class.php';
  10.     require __SITE_PATH . '/core/' . 'quickdb.class.php';
  11.  
  12.     # create new registry object
  13.     $registry = new registry;
  14.    
  15.     # create the database registry object
  16.     $registry->db = new QuickDB($__host, $__user, $__password, $__database);
  17.  
  18.     # load the router
  19.     $registry->router = new router($registry);
  20.    
  21.     # load the model
  22.     $registry->model = new model($registry->db, $registry->router->getFile());
  23.    
  24.     # load up the template
  25.     $registry->template = new template($registry);
  26.    
  27.     # load the controller
  28.     $registry->router->dispatch();
  29.    
  30.     ### end of the index file ###

y en la clase para los controladores usa algo asi:
Código PHP:
Ver original
  1. abstract class Controller
  2.     {
  3.         protected $registry; # protected so that it is accessible from other classes as well.
  4.        
  5.         function __construct($registry)
  6.         {
  7.             $this->registry = $registry;
  8.         }
  9.  
  10.         # all controllers must contain an index method
  11.         abstract function index();
  12.  
  13.         protected function redirect($url)
  14.         {
  15.             header('LOCATION: ' . html_entity_decode($url));
  16.             exit();
  17.         }
  18.        
  19.     }

y en los controladores del sistema asi:
Código PHP:
Ver original
  1. class error404Controller extends Controller
  2.     {
  3.         public function index()
  4.         {
  5.             $this->registry->template->blog_heading = '404 Error - Page Not Found !';
  6.             $this->registry->template->show('error404');
  7.         }
  8.     }
la verdad que pense que era mas complicado al ver lo que era la documentacion de como lo usa zend jejej