1

What are the drawbacks to using a class with only static methods in PHP as a wrapper for common functions? Especially if I don't need more than one of something, or I want some sort of global controller.

For example:

class SomeClass{
    private static $somedata = null;

    public static function setdata($value)
    {
        self::$somedata = $value;
    }

    public static function getdata()
    {
        return self::$somedata;
    }
}

class AnotherClass{
    public static function modifydata($value)
    {
        someClass::setdata($value);
    }
}

SomeClass::setdata('firstval');

// returns 'firstval'
echo SomeClass::getdata();

AnotherClass::modifydata('secondval');

// returns 'secondval'
echo SomeClass::getdata();

I don't see this done commonly, and it seems like a pretty simple way to handle things, or am I just completely off base? What are the advantages / disadvantages to approaching things this way when building PHP applications?

Saturnaut
  • 11
  • 2
  • That's OOP design. It keeps things clear as to what class the function is coming from, and I don't see anything wrong with it. – echolocation Mar 21 '14 at 16:32
  • 2
    http://stackoverflow.com/questions/2080150/when-should-i-use-static-methods-in-a-class-and-what-are-the-benefits – Abhik Chakraborty Mar 21 '14 at 16:33
  • Thanks @abhik-chakraborty, I did some searching but I wasn't coming across anything concrete. – Saturnaut Mar 21 '14 at 16:40
  • Thanks @echolocation I figured as much, but I wasn't sure if there were any disadvantages to handling classes like this, or if it was better / faster / whatever to always instantiate the class as an object. – Saturnaut Mar 21 '14 at 16:42
  • Static functions are difficult to unit test using PHPunit. – Revent Sep 16 '15 at 00:49

0 Answers0