I am trying to find a good practice for storing commonly shared object for my framework. Previously, I am using $GLOBALS
for accessing those shared object. However, I find many articles on internet saying that using $GLOBALS
is a bad practice and suggest using Registry Pattern.
However, I have difficult time seeing how Registry Pattern is a better solution than $GLOBALS
. For example:
<?php
$config = new Config();
$config->autoAppPath(__DIR__);
$config->option = ... // some value;
$config->option2 = ... // some value;
$db = new Database();
Registry::set('config', $config);
Registry::set('db', $db);
?>
I wonder how is accessing using Registry is better than using GLOBALS asides from it has a better namespace?
Registry::get('config');
$GLOBALS['config'];