• 5

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

When to use static modifier in PHP

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?

    • 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.

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)

  • 23
Reply Report
      • 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.

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.

  • 7
Reply Report
      • 1
    • 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.
      • 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 kunststube.net/static for more elaboration.
      • 2
    • 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.
      • 2
    • 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; stevedrivendevelopment.com/2012/08/24/statics-and-unit-testing
      • 2
    • 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.

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.

  • 4
Reply Report

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.

  • 3
Reply Report

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.

  • 2
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions