Debunking The Myth Of Static Classes, Methods and Variables

Devin Dixon
Dec 12, 2018 · 4 min read

Urban Legends and myths. It starts with Santa Claus when we are children to when we become adults and believe in the 5-second rule (some readers will die by it regardless of what science says). Myths proliferate into programming as well, including languages like PHP.

One such myth is the double vs single quotes in performance, which has been disproven many times. Another myth is the use of static methods is bad when rather the approach is misunderstood. Below are going over common misconceptions of using static methods in PHP, along with the real pros and cons.

Does Not Support Inheritance

FALSE. In PHP, static methods and variables have the same visibility operators as methods and variables instances do. This means public, private and protected act the same. For example:

class Parent {
public static $var1 = ‘ABC’;

Extend our example parent class to a child:

class Child extends Parent {

And then we can echo a few outputs:

//Will Work
echo Child::var1;

While we are using class variables to illustrate the example, the same rules apply to methods. This mean methods to belong to classes as some of them will only be able to access functionality in that class. So if inheritance works, what else does with static?

Does Not Support Polymorphism

FALSE. How we think about Polymorphism can change with the capabilities of each language. Polymorphism in Java with overloading a method is not the same kind of Polymorphism achieved with using PHP interfaces.

In PHP, polymorphism is achieved through interfaces or abstract classes because they enforce a required class structure among all the children classes. An example:

interface MyInterface {
public function doThis();
public function doThat();
public function setName($name);
}

Now the same functionality can be achieved through static methods. The Helium SessionService is an example that sets up interfaces using static methods:

With this, we now can achieve Polymorphism by having all children classes required to enforce methods above. We will get into a use case later on how this relates to testing.

Side Effects & Hidden Dependencies

FALSE. A method being static or not has no bearing on its testability or is more susceptible to side effects, especially if it does not have a state. An example:

class Math {
public static function add($a, $b) {
return $a +$b;
}
}

This will have the same testability if we did functional programming:

$add = function($a, b) {
return $a +$b;
}

When ran through testing frameworks like PHPUnit, both functions will deliver the same result. Testing becomes more difficult with changing states that cause side effects, which is an object-oriented problem regardless if its an instance or static. Same for hidden dependencies, its how you design your application and is not related to static methods.

Tightly Coupled and Cannot Be Tested

FALSE. In this answer, we are going to start combining some of the different points discussed above. We’ve proven that with static methods we can have interfaces and abstract classes which creates the ability to have an inversion of control. This means loose coupling and testing different cases.

Going back to our example of the session class from Helium, we can throw in a class that injects a Redis Session Handler. Using the code below as an example:

SessionService For Dependency Injection

RedisSession To Be Injected In SessionService

Now with dependency injection, we can create an inversion of control like so:

//Create an instance of Redis
$redis = new Redis();

This can be put into a one-liner, its broken up for explanation purposes. Tie this in which the ability to swap session handing with Redis, with Cookies, or the database, we have the ability to test different interfaces as long as the SessionService class is the one being called. Again, static does not create a tight coupling and impede testing if implemented correctly.

The Real Pros and Cons of Static is The State

Static has several benefits that include memory and performance that are touched on in-depth in this article:

When To Use Static Classes And Methods

To briefly summarize, static is appropriate when:

  • A state is not important in the execution of the object
  • There can be signification improvement from not instantiating an object
  • When memory can be shared to improve performance
  • In Microservices

Bottom Line

Static variables and methods are not bad. They have their place and use cases which depends on how you want to design your application. But most of what can be achieved with instances can be with the use of static as well.

Helium MVC

The blog of the Helium MVC and ProdigyView Toolkit

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store