The codebase I\'ve been handed to work with features a databse class that inherits from MDB2. This forms the basis for the MVC framework in use (a custom built affair) and t
Since you did not provide any code yet, this is a blind suggestion how you could remove the inheritance with very little code, while at the same time, maintaining full functionality and making sure the MDB class is only instantiated once.
class Db
{
protected static $_mdb;
public function __construct()
{
if(self::_mdb === NULL) {
self::_mdb = new MDB;
}
}
public function __call($method, $args)
{
return call_user_func_array(array(self::_mdb, $method), $args);
}
}
This will basically make your DB class a decorator for MDB. On first instantiation, the DB class will create and store a static instance of MDB. This will be shared among any instances of DB, including child classes. There is no reason to use a Singleton here.
The __call
interceptor will make sure any methods that you called in DB that call methods on MDB method will be caught and delegated to the MDB instance. Magic methods can have a severe performance impact, so when you notice any performance impact, add any called methods to the DB class and delegate from there.
Needless to say, this is still not the best solution, because your DB instance is still tightly coupled to your model classes. If you can afford more refactoring, I'd suggest to make all the classes that currently inherit from DB encapsulate the DB instance instead (unless they are ActiveRecords). Then use Dependency Injection to make the DB instance available.