views:

62

answers:

2

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

If an example would help, here is a PHP class I use to access a database (I don't think this is a PHP-specific question, though):

<?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();
    }
}

?>
+1  A: 

I would think that what you read was related to testing those classes.

For a good indepth explanation on this and much more, I'd suggest you read Misco's blog. I have found them to be a valuable source of information.

The basic issue with static methods is they are procedural code. I have no idea how to unit-test procedural code. Unit-testing assumes that I can instantiate a piece of my application in isolation. During the instantiation I wire the dependencies with mocks/friendlies which replace the real dependencies. With procedural programing there is nothing to “wire” since there are no objects, the code and data are separate.

Lieven
I think its a two part issue dealing with mocking static classes or mocking classes that have static methods. Mainly because you cant interface them.
Nix
+4  A: 

As long as your static class has methods and no data, it's just a namespace. No problem there. But if you have static data, you run into the same problems as global variables: you can no longer understand the behavior of the system from looking at local information. Particularly in a multi-threaded environment, you could be in for unexpected behavior and difficult debugging.

John D. Cook
@John - That makes a lot of sense!
letseatfood