Skip to content

Instantly share code, notes, and snippets.

@cmstone
Created July 22, 2013 16:54
Show Gist options
  • Save cmstone/6055518 to your computer and use it in GitHub Desktop.
Save cmstone/6055518 to your computer and use it in GitHub Desktop.
Registry Pattern Example
<?php
class Registry {
private $registry = array();
private static $instance = null;
public static function getInstance() {
if($this->instance === null) {
$this->instance = new Registry();
}
return $this->instance;
}
private function __construct() {}
private function __clone() {}
public function set($key, $value) {
if (isset($this->registry[$key])) {
throw new Exception("There is already an entry for key " . $key);
}
$this->registry[$key] = $value;
}
public function get($key) {
if (!isset($this->registry[$key])) {
throw new Exception("There is no entry for key " . $key);
}
return $this->registry[$key];
}
}
$mysqli = new mysqli();
$mysqli->connect('localhost', 'luminusm', 'NiX8VWe5', 'luminusm_tedesco');
$registry = Registry::getInstance();
$registry->set('mysqli', $mysqli);
@thanhtutoo
Copy link

can u please writing the comments of ur codes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment