35

Doing some code reviews lately I came across a number of classes that have significant number of static methods in them... and I can't seem to grasp why? Hence my question:

What are the best practices regarding using static methods in PHP?

When would one want to use them and when would one shouldn't use them?

What are specific difference in how runtime handles static methods? Do they affect performance or memory footprint?

hakre
  • 184,866
  • 48
  • 414
  • 792
Alex N.
  • 13,499
  • 10
  • 45
  • 52
  • 1
    No one has answered the last question till now -- "Do they affect performance or memory footprint?" Static's are generally discouraged in Java. Not sure this is also true for interpret language like PHP. – powerboy Sep 10 '11 at 20:39
  • if you are doing OOP (as you have tagged), then never: http://stackoverflow.com/questions/5167660/static-method-get-is-this-bad-practice/23386464#23386464 – Alejandro Moreno Apr 30 '14 at 11:22

7 Answers7

23

Doing some code reviews lately I came across a number of classes that have significant number of static methods in them... and I can't seem to grasp why

PHP didn't have namespaces before 5.3, so all function/variables would be in global scope unless they belonged in some class. Putting them in a class as static members is a workaround for not having namespaces (and that's probably why you saw them in "significant" number)

Generally, they are used for functions that aren't much useful in individual objects, but has some use at class level (as said in other answers)

Imran
  • 81,919
  • 23
  • 95
  • 127
  • 2
    Organizing related functions as statics in classes also explore a powerful feature on PHP: The ability to autoload classes on demand. If allows you to write your code without worrying about doing the proper includes, and without need to include something you might not always use. You simply call your static method and the `__autoload()` does its magic. Clean, optimized and functional coding. Not even namespaces got that one yet. – Havenard Apr 19 '13 at 17:52
7

The best practice is avoid using them whenever possible, because they kill testability and maintainability. Two great reads that elaborate:

CLARIFICATION: There seems to be a lot of misunderstanding on this issue. Lack of dependency injection is the real problem. Directly calling static methods just happens to be the one of the most common ways of falling into that trap.

Russell Davis
  • 7,948
  • 4
  • 37
  • 41
  • 3
    I've seen these arguments before, but they're not compelling. The way you make static methods testable is by having no side effects; such methods are the most testable of all; you give them an input, they return an output, that's it. The hard dependency on a class argument carries no weight either; grouping related static methods together in a class is the whole point. – Robert Harvey Dec 11 '12 at 15:17
  • 1
    You didn't address the crux of both of those links -- *dependencies*. Being able to override depended-on functions is crucial to testing. To be fair, it is *possible* to do this with static functions (e.g., by taking function dependencies as parameters). However, the idiomatic solution in PHP is to use a non-static function that can be overriden by a subclass. See http://kunststube.net/static/ for more elaboration. – Russell Davis Dec 12 '12 at 05:29
  • 1
    Properly written static functions *don't have any dependencies*, except that which is passed directly into the function. Test the function, make sure it has the expected behavior, then use it in all other tests as-is. The examples provided at your link are a mis-use, as calling the functions more than once can produce different results each time. I don't know; I just don't see the benefit of using `static` methods that way. – Robert Harvey Dec 12 '12 at 05:30
  • 1
    It's not dependencies *of* statics that are the problem. It's dependencies *on* statics. From "Static Methods are Death to Testability": There seems to be a common misreading of the original article, where Misko says something like ‘if you use a static in a class, that class is harder to unit test’ and people hear ‘statics are hard to unit test’. It’s the classes ‘infected’ by statics that are hard to test, not the static itself; http://stevedrivendevelopment.com/2012/08/24/statics-and-unit-testing/ – Russell Davis Dec 12 '12 at 05:42
  • *"in order to write a test for the dictionary, I need to know the implementation details of the static to write my test"* -- Game over. A proper use of a static method would not require knowledge of its internal implementation. I think most folks who complain about the difficulties of testing using `static` methods are often simply using `static` methods improperly. More here: http://programmers.stackexchange.com/a/5963/1204 – Robert Harvey Dec 12 '12 at 05:59
  • 1
    Agreed. Looking closer at that code, it could just as easily be setting `this.hasher` to a static hashing function, which would solve the testability problem equally as well. – Russell Davis Dec 12 '12 at 06:13
  • I updated my answer to clarify. To be clear, I still advocate that directly calling static methods without providing for dependency injection is death to testability. – Russell Davis Dec 12 '12 at 07:35
5

Static methods are used for

  • functions that are related to the whole collection of objects of the given class (e.g. singleton pattern)
  • functions that are not related to anything, but must be put under a class because of OO (e.g. utility classes)
Zed
  • 55,616
  • 9
  • 73
  • 100
4

Static method doesn't require an instance (and may return one instead) and is more or less like a global function except for it is put in class' namespace (and therefore avoid collisions with other functions) and has access to class' private members.

So, use it whenever you're interested in these properties of the function.

Michael Krelin - hacker
  • 131,515
  • 23
  • 189
  • 171
3

There is nothing PHP specific about the use of static methods.

Static methods can be called directly on the class - no need for an instantiated object.

So their main use is for methods that are related to the classes functionality, but do not need an existing instance to be of use for other code.

A common example would be a custom comparison method that can be passed to, say, the uasort() function to sort an array of objects of the class' type.

Henrik Opel
  • 19,223
  • 1
  • 47
  • 62
2

you can use static methods for better performance. you don't need to create object for each user that using your web App and creating object with multiple methods and properties is slower and needs much more system resources.

Jquest
  • 89
  • 1
  • 2
1

You don't need to create an instance of the class to use its static methods.

Havenard
  • 25,140
  • 4
  • 32
  • 60