Jokes IRL: A man and a genie.


There is an old joke:

Some Man got lost deep in a desert. Exhausted to the last degree and losing all the hope, he suddenly finds a lamp. He rubs it and naturally, n a puff of smoke Genie emerges, offering to grant a wish as a sign of gratitude. So Man wishes:
-- I want to get home!
-- No problem! -- answers Genie -- Let's go! -- and starts walking.
Following him a few steps, Man suddenly stops:
-- Hey! I want to get home quick!
-- As you wish, buddy! -- says Genie -- Let's run!

You may find it funny or not, but it always fun to find a code that tells you literally the same story. Recently I came across a snippet that is intended to be an example of a singleton pattern for a database access. I don't know what made the author to write it - may be it's a result of the oversimplification in pursuit for making it easier to understand, or may be it was intentionally made this way, sort of tongue-in-a-cheek: an attempt to sell you vanilla PDO connection in a disguise of a singleton. But it seems that people tend to take it literally. So here it goes:

For brevity, I'll copy it here, leaving only meaningful parts:

class PDOConnection
    protected static 
$_instance null;

// Well by itself it's a first class singleton, keeping its own instance
public static function instance()
        if ( !isset( 
self::$_instance ) ) {
self::$_instance = new PDOConnection();

// But... for the database connection, you just create it every time anew!
public function getConnection($dsn$username$password)
$conn = new \PDO($dsn$username$password);


So, in theory we have a Genie that brings us PDO instance anywhere we need it.

In reality we have to walk all the way home by foot, creating a new PDO instance every time we need it.

Related articles: