I once read that static classes are very difficult and even impossible to debug. Is this true and why?

I once read that static classes are very difficult and even impossible to debug. Is this true and why?

If the example helps, here is the class PHPI'm using to access the database (I don't think this is a PHP specific issue):

<?php

class DB
{
    private static $instance;

    private function __construct() { }

    public static function getInstance()
    {
        if(!self::$instance)
        {
            self::$instance = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';', DB_USER, DB_PASS);
        }
        return self::$instance;
    }

    public static function getPreparedStatement($query)
    {
        $db = self::getInstance();
        return $db->prepare($query);
    }

    public static function query($query)
    {
        $stmt = self::getPreparedStatement($query);
        $stmt->execute();
    }

    public static function getResult($query)
    {
        $stmt = self::getPreparedStatement($query);
        $stmt->execute();
        return $stmt;
    }

    public static function getSingleRow($query)
    {
        $stmt = self::getPreparedStatement($query);
        $stmt->execute();
        return $stmt->fetch();
    }

    public static function getMultipleRows($query)
    {
        $stmt = self::getPreparedStatement($query);
        $stmt->execute();
        return $stmt->fetchAll();
    }
}

?>
+5
source share
2 answers

, . . , , : . , .

+4

, .

, Misco. .

: . . , . mocks/friendlies, . "", , .

+1

All Articles