I am rather new to the concepts of design patterns and I'm thinking of using Dependency Injection and Polymorphism (as each are applicable) -- but I have many Singletons and while most of them can easily be changed, my DBAL cannot.
The reason why is the DBAL creates a connection to the Database -- sets up it's own PDO object. If I passed new DBALs to every class that needs it (quite a few) I would get multiple, unnecessary connections to the database.
The class is something like this
class DB { /** * Hold the PDO object * @var PDO */ private $_db; /** * Hold the last error messages * @var string */ private $_error_message = NULL; /** * Hold the last error code * @var int */ private $_error_code = NULL; /** * Connects to the database server and selects a database * * @param string $user MySQL database user * @param string $password MySQL database password * @param string $name MySQL database name * @param string $host MySQL database host * @return bool */ public function connect( $user, $password, $name, $host ) { // Connect try { $this->_db = new PDO( "mysql:host=$host;dbname=$name", $user, $password ); } catch ( PDOException $e ) { $this->_error_message = $e->getMessage(); $this->_error_code = $e->getCode(); return false; } return true; } // ... } ?> There will be many classes that inherit this class -- what is the best way to handle this? (I am new to design patterns)
newexcept for in clear factories).