When To Use Static Classes And Methods

Devin Dixon
Helium MVC

--

Have you ever read something about programming that was outdated or misunderstood and cringed? Many PHP developers can probably relate when they hear misinformation such as “Python has better package management”, “PHP is a slow scripting language”, “The language is not object-oriented”, fractal bad design, etc etc. What is worse is that many of these misconceptions rank first on Google. The use of static has the same problem.

This is part 2 of an article about the use of static class and methods in PHP. The first section of the article that debunked erroneous misconceptions about static was published here:

Debunking The Myths Of Static Methods and Variables

To recap some of the articles points, using static you can:

  1. Have inheritance and public/protected/private accessors function the same.
  2. Can use create polymorphism with Interfaces and Abstract classes
  3. Static Methods are testable.
  4. Have inversion of control with dependency injection.
  5. Unknown side effects are from the programmer, not for a method being static. OOP can have side effects as wells.

Now we can dive into the benefits of static classes, methods and variable, and when to use them.

--

--