Is it acceptable to call initialization methods inside a static class to avoid using an autoloader?

28 views Asked by At

Say I have a case where I need a helper class that should have static methods, but those methods need a static object to manipulate.

I would rather have the helper be self-contained, so I only have to include the file. Alternatively, one could add the initialization call to some kind of autoloader, but I would rather avoid that.

Considering that you can't use a __construct method here because the class is never instantiated, is this an acceptable alternative?

class HelperClass
{
  private static $property;
  private static function init()
  {
    if (!isset(self::$property))
      self::$property = new stdClass;
  }
  static function addSomething($key, $value)
  {
    self::$property->$key = $value;
  }
  static function getObject()
  {
    if (isset(self::$property))
      return self::$property;
    else
      return new stdClass;
  }
}
0

There are 0 answers