As a PHP developer, you may have encountered static methods in your code before. These are methods that are defined as static and can be called directly on a class, rather than on an instance of the class.

While static methods can be convenient in some situations, they are generally considered to be a bad practice in object-oriented programming (OOP). In this article, we'll discuss why you should avoid using static methods in your PHP code.

1. Static methods are not polymorphic

One of the key principles of OOP is polymorphism, which allows you to create a hierarchy of classes that share a common interface but have different implementations. This makes it easy to create flexible and extensible code that can be easily modified and maintained.

However, static methods are not polymorphic. This means that they cannot be overridden in child classes. This can make it difficult to create flexible and extensible code, as you cannot easily extend or modify the behavior of a static method.

2. Static methods are not as testable as non-static methods

In order to write effective unit tests, it's important to be able to mock or stub out certain parts of your code. This allows you to test the behavior of a specific component in isolation, rather than relying on the behavior of other parts of your code.

However, static methods are not associated with an object, so it can be difficult to mock or stub them in tests. This can make it more difficult to write effective unit tests for code that relies on static methods.

3. Static methods can make it difficult to reason about the state of an application

When working with OOP, it's important to understand the state of an object at any given time. This can help you identify problems or bugs in your code, as well as make it easier to understand how your code is functioning.

However, since static methods are not tied to a specific object, it can be difficult to determine the state of an object when a static method is called. This can make it more difficult to reason about the state of your application, which can make it more difficult to identify and fix problems.

4. So, should you never use static methods in PHP?

While static methods have their uses, they should generally be avoided in OOP. Non-static methods, on the other hand, are much more flexible and testable, and can make it easier to reason about the state of an object.

To make things a bit easier to understand, we have prepared a code example for you to read. This way, you will be able to learn the differences even a bit better:

php
<?php

// Static method example
class MathUtils {
  public static function add($a, $b) {
    return $a + $b;
  }
}

echo MathUtils::add(1, 2); // 3

// Non-static method example
class MathUtils {
  public function add($a, $b) {
    return $a + $b;
  }
}

$mathUtils = new MathUtils();
echo $mathUtils->add(1, 2); // 3

In this example, we have a `MathUtils` class with a static add method that takes two numbers as arguments and returns their sum. We can call this static method directly on the class, like so: `MathUtils::add(1, 2)`.

To replace this static method with a non-static method, we simply remove the static keyword and define the add method as a regular instance method. Then, we can create an instance of the `MathUtils` class and call the add method on that instance, like so: `$mathUtils->add(1, 2)`.

This non-static approach has several advantages over the static approach. For example, the non-static method can be overridden in child classes, it can be more easily mocked or stubbed in tests, and it is tied to a specific object, which can make it easier to reason about the state of the application.


That being said, there are certainly situations where static methods can be useful. For example, if you need to create a utility function that does not depend on the state of any particular object, a static method may be a good choice. However, in general, it is a good idea to use non-static methods wherever possible and reserve static methods for specific cases where they are necessary.

In conclusion, while static methods can be convenient in some situations, they are generally considered to be a bad practice in OOP. By avoiding static methods in your PHP code, you can create more flexible, extensible, and testable code that is easier to reason about and maintain.