When to use static vs instantiated classes

This is quite an interesting question — and answers might get interesting too ^^

The simplest way to consider things might be :

  • use an instanciated class where each object has data on its own (like a user has a name)
  • use a static class when it’s just a tool that works on other stuff (like, for instance, a syntax converter for BB code to HTML ; it doesn’t have a life on its own)

(Yeah, I admit, really really overly-simplified…)

One thing about static methods/classes is that they don’t facilitate unit testing (at least in PHP, but probably in other languages too).

Another thing about static data is that only one instance of it exists in your program : if you set MyClass::$myData to some value somewhere, it’ll have this value, and only it, every where — Speaking about the user, you would be able to have only one user — which is not that great, is it ?

For a blog system, what could I say ? There’s not much I would write as static, actually, I think ; maybe the DB-access class, but probably not, in the end ^^

Leave a Comment