Advertisement
Avatarr

singleton

Jun 12th, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.28 KB | None | 0 0
  1. <?php
  2.  
  3. abstract class singleton {
  4.     protected static $instance;
  5.     final protected function __construct() {
  6.         $this->construct();
  7.     }
  8.  
  9.     abstract protected function construct();
  10.         abstract protected function construct($_str);
  11.  
  12.     final public static function getInstance() {
  13.         if (!isset(static::$instance)) {
  14.             $className = get_called_class();
  15.             $new_instance = new $className();
  16.             if (!isset(static::$instance)) {
  17.                 static::$instance = $new_instance;
  18.             }
  19.         }
  20.         return static::$instance;
  21.     }
  22.  
  23.     final public function __clone() {
  24.         trigger_error("Unable to clone singleton class __CLASS__", E_USER_ERROR);
  25.     }
  26.  
  27.     final public function __wakeup() {
  28.         trigger_error("Unable to unserialize singleton class __CLASS__", E_USER_ERROR);
  29.     }
  30. }
  31.  
  32. ---------------------------- call function
  33.  
  34.  
  35. <?php
  36.  
  37. require_once "util/singleton.class.php";
  38.  
  39. class users extends singleton
  40. {
  41.     protected static $instance;
  42.     private $apple_id = array();
  43.  
  44.     protected function construct($_str)
  45.     {
  46.         //echo "users run!!!";
  47.         $this->setUser($_str);
  48.     }
  49.  
  50.     function getUser()
  51.     {
  52.         return $this->apple_id;
  53.     }
  54.  
  55.     function setUser($_str)
  56.     {
  57.             //$result = mysql_query($_str);
  58.             //$this->apple_id = mysql_fetch_assoc($result);
  59.             echo $_str;
  60.     }
  61.        
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement