Modifying a class to encapsulate instead of inherit?

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.

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.

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. 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.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions