I have a custom framework where i have a class/method which uses my own Cache
class.
Currently it is tightly coupled. So a method instantiates the Cache
class like this:
public function someMethod ( )
{
$cache = new Cache\HHCache();
}
I want ro remove the tight coupling but that's actually where i'm a bit stuck.
I thought it would be a good idea to create some sort of ServiceProvider
class. But i'm not sure if this is really the right approach.
To start i have HHConfig
file which has a static
property in which a cache class is defined. In short:
class HHConfig
{
static $_cacheClass = '\Core\Cache\HHCache';
}
So basically i have a class like this, which is part of the Core functionality of my framework:
interface IHHServiceProvider
{
public function getService ( );
}
Then i have another class which implements this interface
.
class HHCacheProvider implements IHHServiceProvider
{
public static function getService ( )
{
$class = HHConfig::$_cacheClass;
return new $class();
}
}
So now someMethod
can use the HHCacheProvider
class to get an instance of a Cache
class.
public function someMethod ( )
{
$cache = HHCacheProvider::getService ( );
}
My IHHServiceProvider
isn't really like the typical Provider
class since you can't really register any Services
to it. It simply looks in the HHConfig
class what "class" to load and returns in instance of that.
So somehow this method doesn't feel right to me, but i do think it shows what i want to achieve. In what ways can i improve this?
Please note that i'm not looking for a simple Dependency Injection pattern for this. Because i don't want to inject my Cache
class to every constructors class. I need a non tight coupling way of getting an instance of the HHCache
class somehow from within a method.
Some sort of provider class that can be part of my framework seems like the right direction.