Today I have a PHP-project with very strange class structure. Like this:
db_base
`-- db_base_ext extends db_base
+-- module_1 extends db_base_ext
+-- module_2 extends db_base_ext
. ...
+-- module_N extends db_base_ext
`-- db_user extends db_base_ext
class_1
`-- submodule_1_1 extends class_1
db_base
connects to db in __construct()
and have some helper methods.
example of executing:
$db = new db_user();
$user = new user($db);
unset($db);
$db = new module_2();
I don't like that in this code we connects to DB twice and the whole class structure is not really good.
How can I make it better? I mean create only one DB instance and after this work with any class (corresponding to DB)?
Will singleton pattern be good for this?