I've been working my way through the PHP MASTER sitepoint book and have found an issue that affects the singleton, trait and registry sections of the design patterns chapter of the book.
This throws a fatal error: Fatal error: Access level to Database::__construct() must be public (as in class PDO)PHP Code:class Database extends PDO
{
private static $_instance = null;
private function __construct()
{
parent::__construct(APP_DB_DSN, APP_DB_USER, APP_DB_PASSWORD);
}
...
}
I'm ok with the principle that the lesson is teaching, but I'm just curious as to what others do to create a db/model class. Is it better to create the PDO object and pass it into the database object, assigning it to a private property for use?


Reply With Quote





I haven't found a case for a Singleton in several years, but when the scenario arises that requires the above 3 qualifications, I'd go with a singleton instead of playing around with control via an instantiated object. Primarily because it will be much clearer to the other developers from a readability standpoint; versus having to look in the instantiated object and figure out why the behavior seems to be limited -- which it is uncharacteristic of an instantiated object. But that is okay, I don't mind having differing opinions on the subject especially on a rarely (or what should be rarely) used language part.

Bookmarks