Advertisement
Guest User

Singleton

a guest
Oct 23rd, 2014
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.61 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class Ejemplo extends AbstractEjemplo
  5. {
  6.     static private $instance = null;
  7.     // otras propiedades
  8.    
  9.     static public function getInstance()
  10.     {
  11.        if(empty($this->instance)) $this->instance = new Ejemplo();
  12.        return $this->instance;
  13.     }
  14.    
  15.     // el constructor
  16.     private function __construct()
  17.     {
  18.         // tareas del constructor
  19.     }
  20.    
  21.     // otras funciones
  22. }
  23.  
  24. // obtenemos una instancia:
  25. $ejemplo = Ejemplo::getInstance();
  26.  
  27. // ahora ejemplo tiene una instancia de Ejemplo
  28. $ejemplo = Ejemplo::getInstance();
  29.  
  30. // aquí sigue teniendo la misma instancia
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement