Saturday, 29 July 2017

oop - Is there a use-case for singletons with database access in PHP?



I access my MySQL database via PDO. I'm setting up access to the database, and my first attempt was to use the following:



The first thing I thought of is global:



$db = new PDO('mysql:host=127.0.0.1;dbname=toto', 'root', 'pwd');


function some_function() {
global $db;
$db->query('...');
}


This is considered a bad practice. After a little search, I ended up with the Singleton pattern, which





"applies to situations in which there needs to be a single instance of a class."




According to the example in the manual, we should do this:



class Database {
private static $instance, $db;

private function __construct(){}


static function singleton() {
if(!isset(self::$instance))
self::$instance = new __CLASS__;

return self:$instance;
}

function get() {
if(!isset(self::$db))
self::$db = new PDO('mysql:host=127.0.0.1;dbname=toto', 'user', 'pwd')


return self::$db;
}
}

function some_function() {
$db = Database::singleton();
$db->get()->query('...');
}


some_function();


Why do I need that relatively large class when I can do this?



class Database {
private static $db;

private function __construct(){}


static function get() {
if(!isset(self::$db))
self::$db = new PDO('mysql:host=127.0.0.1;dbname=toto', 'user', 'pwd');

return self::$db;
}
}

function some_function() {
Database::get()->query('...');

}

some_function();


This last one works perfectly and I don't need to worry about $db anymore.



How can I create a smaller singleton class, or is there a use-case for singletons that I'm missing in PHP?


Answer



Okay, I wondered over that one for a while when I first started my career. Implemented it different ways and came up with two reasons to choose not to use static classes, but they are pretty big ones.




One is that you will find that very often something that you are absolutely sure that you'll never have more than one instance of, you eventually have a second. You may end up with a second monitor, a second database, a second server--whatever.



When this happens, if you have used a static class you're in for a much worse refactor than if you had used a singleton. A singleton is an iffy pattern in itself, but it converts fairly easily to an intelligent factory pattern--can even be converted to use dependency injection without too much trouble. For instance, if your singleton is gotten through getInstance(), you can pretty easily change that to getInstance(databaseName) and allow for multiple databases--no other code changes.



The second issue is testing (And honestly, this is the same as the first issue). Sometimes you want to replace your database with a mock database. In effect this is a second instance of the database object. This is much harder to do with static classes than it is with a singleton, you only have to mock out the getInstance() method, not every single method in a static class (which in some languages can be very difficult).



It really comes down to habits--and when people say "Globals" are bad, they have very good reasons to say so, but it may not always be obvious until you've hit the problem yourself.



The best thing you can do is ask (like you did) then make a choice and observe the ramifications of your decision. Having the knowledge to interpret your code's evolution over time is much more important than doing it right in the first place.



No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...