I am currently working on a class to make objects and vars available to child classes while attempting to avoid a singleton. Each child with dependencies extend this class and pass params (var names) of required dependencies. If parent construct is called and params is empty full registry is set.
Example of Registry
:
class Registry {
public static $registry = [];
public function __construct($DI = '') {
if (self::$registry) {
/**
* $DI List Missing Pass Entire Registry
*/
if (!$DI) {
foreach (self::$registry as $var => $value) {
$this->$var = $value;
}
}
/**
* $DI List Set, Pass Specific Dependencies
*/
else {
foreach ($DI as $dependency) {
if (isset(self::$registry[$dependency])) {
$this->$dependency = self::$registry[$dependency];
}
else {
if (ENVIRONMENT == 'development') {
echo get_called_class().' Needs Dependency '.$dependency.'<br /><br />';
}
}
}
}
}
}
/**
* Add to Registry
*/
public static function add($array, $type = 'class', $namespace = '') {
foreach ($array as $var => $value) {
if (!isset(self::$registry[$var])) {
switch ($type) {
case 'class':
$class = $namespace.$value;
self::$registry[$var] = new $class;
break;
case 'var':
self::$registry[$var] = $value;
break;
}
}
}
}
private function __clone() {}
private function __wakeup() {}
}
Adding and using vars/objects from registry is easily done through static calling, but the main point of this class is to pass dependencies to child classes. I am wondering if there are drawbacks that someone with more experience would know of/find.