Reputation:
What is the difference between Registry pattern and $_ENV, especially for using in php?
Which of them provide more security and performance?
For example why many coders use $config['deflang'] and Registry pattern instead of just $_ENV['deflang']
Thanks in advance
Upvotes: 1
Views: 195
Reputation: 577
try this:
class Registry {
public static $instance;
public function set($key, $val) {
$this->_reg[$key] = $val;
}
public function get($key) {
return $this->_reg[$key];
}
public static function Singleton() {
$class = __CLASS__;
if (!(self::$instance instanceof $class)) {
try {
if (!defined('REQUIRED')) {
throw new Registry_Exception('No direct access.');
}
} catch (Registry_Exception $e) {}
self::$instance = new $class();
}
return self::$instance;
}
private function __construct() {}
}
$registry = new Registry();
$registry->set('setting', 'value');
Upvotes: 0
Reputation: 32155
Registry pattern allows you to lazy-load resources.
$db = $_ENV['db_connection']; // The connection must be setup prior
vs
$db = $config->get('db_connection');
// An internal method can check for an existing connection
// and set one up if needed
Upvotes: 0
Reputation: 318508
Those two things are completely different.
$_ENV
is a superglobal array containing all environment variables.$config
is some user-defined variable, could be from a config file, a database, etc.A common way (especially in some frameworks) is to have a config file that contains an array with multiple configs and one environment variable (e.g. FRAMEWORKNAME_ENV
) which then selects the active config.
Upvotes: 0
Reputation:
$_ENV
has a very specific purpose -- it's the process environment. You're not really supposed to throw random data into it. If you want to do that, at least use a global (or, better, a static class member).
Upvotes: 2