The pattern is pretty alright. I do have a few questions/comments on it.
First currently it seems that the way you handle missing dependency is to just return an empty string, this will cause you tons of problems down the line, ideally you would like to throw some sort of RuntimeException that the dependency is not currently instantiated
or
. Or, an even better approach would be to lazy instantiate dependencies as you need them, that way you don't have to register all your classes in the "bootstrap" but you will only wire the dependencies you need per request.
In your case your dependency resolve method looks like:
public function get($var) {
if (isset(self::$container[$var])) {
return self::$container[$var];
}
return '';
}
}
which assumes you are wiring all dependencies in every request, and then only injecting the needed ones, a more optimal solution would be to only lazy load the ones you actually need per request.
Remember that PHP doesn't have true singletons that span across requests, everything is ran in a request scope.
A better alternative would be something like:
public function get($var) {
if (isset(self::$container[$var])) {
return self::$container[$var];
}else{
return tryToInitDependency($var); //throws RuntimeException if you cant instantiate it
}
return null;
}
}
My suggestion disregards the fact that dependencies might depend on other dependencies and the problem with circular dependencies.
A part from that the core Registry you have is pretty good. Hope that helps.