Using PHP namespaces instead of static methods

Since version 5.3 namespaces have been available in PHP. Although this is mostly associated with object oriented programming (OOP), namespaces can be used with procedural functions. But this seems not to be widely known.

I often see people using classes to organize code. This is logical if you want to utilize the possibilities OOP has to offer. But I never liked classes that existed only of static methods. To me it always felt like pretending to do OOP, but in reality still working procedurally.

In such cases I preferred to use prefixes, which accomplishes the same thing. With namespaces you now can use a proper native construct.

So below you can compare the different solutions and decide which make more sense to you. But for me, classes should be used with OOP and not procedural code.

Static methods


class mystuff
{
  public static function doSomething()
  {
    return "Do something!";
  }
}

echo mystuff::doSomething();

Prefixes


function mystuff_doSomething()
{
  return "Do something!";
}

echo mystuff_doSomething();

Namespaces


namespace mystuff;

function doSomething()
{
  return "Do something!";
}

echo \mystuff\doSomething();