Php – Abstract classes with static methods in PHP

abstract classdesign-patternsPHPstatic-access

Pre: I am looking at a quite big PHP project developed by a company which we need to partly interact with while building a new webapp.
The whole application is developed in PHP 5.

Most of the classes in this project, and in particular all the classes that interact with the database, are declared abstract and contain only static functions.

Take for example this file:

abstract class DataHelper_Command
{

    /**
     * @param unknown $id_device
     * @param unknown $latest_command_id
     * @return Command[]
     */
    public static function getCommandsToSend($id_device, $latest_command_id)
    {
        $rows = DBUtility::executePreparedQuery('SPS_COMMANDS_TO_SEND', array(":id_device" => $id_device, ":latest_command_id" => $latest_command_id));
        return DataHelper_Command::commandsFromDataTable($rows);
    }

    public static function getFW()
    {
        $rows = DBUtility::executePreparedQuery('SPS_FW_VERSION');
        return $rows;
    }

    /**
     * @param unknown $rows
     * @return Command[]
     */
    private static function commandsFromDataTable(&$rows)
    {
        $res = array();
        foreach ($rows as &$row)
        {
            $res[] = DataHelper_Command::commandFromDataRow($row);
        }

        return $res;
    }

    /**
     * @param array $row
     * @return Command
     */
    private static function commandFromDataRow(&$row)
    {
        return new Command($row["id_command"], $row["type"], $row["text"]);
    }
}

I don't even see any trace of using a Singleton pattern in the project!

That sounds a bit strange to me, as I always heard that using static classes is a nightmare in terms of performance.
This server was meant to be able to run on both standard webservers and on a really performance-limited embedded device (An ARM running at 400MHz, with 128 MB of RAM).

Am I missing something here? Are there any use-cases in which this is considered a good practice?

Best Answer

Performance is not even the biggest problem here, secret dependencies and lying APIs are.

From a testable point of view, static methods are awful, because they usually represent global state, which, surprisingly, is awful as well, because it means if you run a function two times, you can get different results even when the inputs are the same (that is what global state does). How do you test that?

I cannot really see any case, where I would prefer static methods instead of normal ones. If you can, do not use them, if you must use them, your design is most likely wrong and needs refactoring.

Check Clean Code Talks by Miško Hevery from 2008, they are still relevant and provide great explanations on why static methods and singletons should be avoided.


Some youtube references to some talks by Miško Hevery: