Ver Mensaje Individual
  #11 (permalink)  
Antiguo 30/08/2010, 12:35
atrianaster
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Duda con PDO y mi clase

@GatorV gracias por tus recomendaciones, aquí les dejo la clase terminada.

Código PHP:
Ver original
  1. /**
  2. * Registers objects and variables
  3. *
  4. * Makes objects and variables available to any level
  5. * of the application without having to keep track
  6. * of their existence.  Also useful for objects such
  7. * as database connectors that are used globaly and
  8. * not to be duplicated.
  9. *
  10. * PHP version 5
  11. *
  12. * @author Arley Triana Morín <[email protected]>
  13. */
  14. class Registry
  15. {
  16.     /**
  17.      * Registry of variables and objects
  18.      * @access static private
  19.      * @var array
  20.      */
  21.     static private $registry = array();
  22.  
  23.     /**
  24.      * Adds an item to the registry
  25.      * @access static public
  26.      * @param string item's unique name
  27.      * @param mixed item
  28.      * @return boolean
  29.      */
  30.     static public function add($name, $item)
  31.     {
  32.         if (!self::exists($name)) {
  33.             self::$registry[$name] = $item;
  34.             return true;
  35.         } else {
  36.             return false;
  37.         }
  38.     }
  39.  
  40.     /**
  41.      * Returns true if item is registered
  42.      * @access static public
  43.      * @param string item's name
  44.      * @return boolean
  45.      */
  46.     static public function exists($name)
  47.     {
  48.         if (is_string($name)) {
  49.             return array_key_exists($name, self::$registry);
  50.         } else {
  51.             throw new Exception('Registry item\'s name must be a string');
  52.         }
  53.     }
  54.  
  55.     /**
  56.      * Returns registered item
  57.      * @access static public
  58.      * @param string item's name
  59.      * @return mixed (null if name is not in registry)
  60.      */
  61.     static public function get($name)
  62.     {
  63.         if (self::exists($name)) {
  64.             $return = self::$registry[$name];
  65.         } else {
  66.             $return = null;
  67.         }
  68.         return $return;
  69.     }
  70.  
  71.     /**
  72.      * Removes a registry entry
  73.      * @access static public
  74.      * @param string item's name
  75.      * @return boolean
  76.      */
  77.     static public function remove($name)
  78.     {
  79.         if (self::exists($name)) {
  80.             unset(self::$registry[$name]);
  81.         }
  82.         return true;
  83.     }
  84.  
  85.     /**
  86.      * Clears the entire registry
  87.      * @access static public
  88.      * @return boolean
  89.      */
  90.     static public function clear()
  91.     {
  92.         self::$registry = array();
  93.     }
  94. }
  95. ?>

Como Usar?

Código PHP:
Ver original
  1. <?php
  2. require_once 'registry.php';
  3.  
  4. //sets and registers a variable
  5. $item = 'Here is a registered variable';
  6. registry::add('Variable', $item);
  7.  
  8. /**
  9. * Test class that echos a registered variable
  10. */
  11. class Test
  12. {
  13.     private $item;
  14.  
  15.     public function __construct() {
  16.         $this->item = Registry::get('Variable');
  17.     }
  18.  
  19.     public function get() {
  20.         echo '<p>'.$this->item.'</p>';
  21.     }
  22. }
  23.  
  24. //will return "Here is a registered variable"
  25. $test = new Test();
  26. $test->get();
  27.  
  28. //tests if "Variable" exists
  29. if (Registry::exists('Variable')) {
  30.     echo '<p>"Variable" exists</p>';
  31. } else {
  32.     echo '<p>"Variable" does not exists</p>';
  33. }
  34.  
  35. //tests if "variable" exists
  36. if (Registry::exists('variable')) {
  37.     echo '<p>"variable" exists</p>';
  38. } else {
  39.     echo '<p>"variable" does not exists</p>';
  40. }
  41.  
  42. //removes "Variable"
  43. Registry::remove('Variable');
  44. ?>

Última edición por atrianaster; 30/08/2010 a las 14:04 Razón: ortografía