Guest User

Untitled

a guest
Nov 16th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.75 KB | None | 0 0
  1. class Preferences {
  2.     private $props = array();
  3.     private static $instance;
  4.     private function __construct() { }
  5.     public static function getInstance() {
  6.         if ( empty( self::$instance ) ) {
  7.             self::$instance = new Preferences();
  8.         }
  9.         return self::$instance;
  10.     }
  11.     public function setProperty( $key, $val ) {
  12.         $this->props[$key] = $val;
  13.     }
  14.  
  15.     public function getProperty( $key ) {
  16.         return $this->props[$key];
  17.     }
  18. }
  19. $pref = Preferences::getInstance();
  20. $pref->setProperty( "name", "matt" );
  21. unset( $pref ); // remove the reference
  22. $pref2 = Preferences::getInstance();
  23. print $pref2->getProperty( "name" ) ."\n"; // demonstrate value is not lost  it will print matt
Add Comment
Please, Sign In to add comment