Singleton Pattern is designed to restrict instantiation of a class to one object. The pattern can be used for global variables or database connection.
class Preferences {
private $props = array();
private static $instance;
// cannot be instantiated
private function __construct();
public static function getInstance() {
if ( empty( self::$instance )) {
self::$instance = new Preferences();
}
return self::$instance;
}
public function setProperty( $key, $val ) {
$this->props[$key] = $val;
}
public function getProperty( $key ) {
return $this->props[$key];
}
}
No comments:
Post a Comment