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

后端 未结 11 1844
名媛妹妹
名媛妹妹 2020-11-21 06:44

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 glob

相关标签:
11条回答
  • 2020-11-21 07:33

    First, I just want to say that I don't find much uses to the Singleton pattern. Why would one want to keep a single object thorough the whole application? Especially for databases, what if I want to connect to another database server? I have to disconnect and reconnect every time...? Anyway...

    There are several drawbacks to using globals in an application (which is what the traditional use of the Singleton pattern does):

    • Difficult to unit test
    • Dependency injection issues
    • Can create locking issues (multi-threaded application)

    Use static classes instead of a singleton instance provides some of the same drawbacks as well, because the biggest problem of singleton is the static getInstance method.

    You can limit the number of instances a class can have without using the traditional getInstance method:

    class Single {
    
        static private $_instance = false;
    
        public function __construct() {
            if (self::$_instance)
               throw new RuntimeException('An instance of '.__CLASS__.' already exists');
    
            self::$_instance = true;
        }
    
        private function __clone() {
            throw new RuntimeException('Cannot clone a singleton class');
        }
    
        public function __destruct() {
            self::$_instance = false;
        }
    
    }
    
    $a = new Single;
    $b = new Single; // error
    $b = clone($a); // error
    unset($a);
    $b = new Single; // works
    

    This will help on the first the points mentioned above: unit testing and dependency injection; while still making sure a single instance of the class exist in your application. You could, per example, just pass the resulting object to your models (MVC pattern) for them to use.

    0 讨论(0)
  • 2020-11-21 07:38

    In your example you're dealing with a single piece of seemingly unchanging information. For this example a Singleton would be overkill and just using a static function in a class will do just fine.

    More thoughts: You might be experiencing a case of implementing patterns for the sake of patterns and your gut is telling you "no, you don't have to" for the reasons you spelled out.

    BUT: We have no idea of the size and scope of your project. If this is simple code, perhaps throw away, that isn't likely to need to change then yes, go ahead and use static members. But, if you think that your project might need to scale or be prepped for maintenance coding down the road then, yes, you might want to use the Singleton pattern.

    0 讨论(0)
  • 2020-11-21 07:40

    When programming there is not "right" and "wrong"; there is "good practice" and "bad practice".

    Singletons are generally created as a class to be reused later. They need to be created in such a way that the programmer doesn't accidentally instantiate two instances while drunkenly coding at midnight.

    If you have a simple little class that shouldn't be instantiated more than once, you don't need to make it a singleton. It's just a safety net if you do.

    it's not always bad practice to have global objects. If you know that you're going to use it globally/everywhere/all the time, it may be one of the few exceptions. However, globals are generally considered "bad practice" in the same way that goto is considered bad practice.

    0 讨论(0)
  • 2020-11-21 07:43

    You are not missing anything, as far as I can see. The example is pretty flawed. It would make difference, if the singleton class had some non-static instance variables.

    0 讨论(0)
  • 2020-11-21 07:45

    Singletons are considered by many to be anti-patterns as they're really just glorified global variables. In practice there are relatively few scenarios where it's necessary for a class to have only one instance; usually it's just that one instance is sufficient, in which case implementing it as a singleton is completely unnecessary.

    To answer the question, you're right that singletons are overkill here. A simple variable or function will do. A better (more robust) approach, however, would be to use dependency injection to remove the need for global variables altogether.

    0 讨论(0)
提交回复
热议问题