0

Possible Duplicate:
When should I use static methods in a class and what are the benefits?

I'm working in PHP right now.

I'm working on two groups of Functions.

I have a class which consists of Date Handling Functions.

In this class I have no need for properties as each function/method is more or less a utility. As such I have made my class' functions all static.

I have a couple questions from here.

1) What benefits are there from using Static methods? I understand that there is lower processing overhead because there is not an Object. I've also heard this is negligible (depending).

2) What other types of functions/methods would be good candidates for "static" besides utilities?

Thanks

Community
  • 1
  • 1
mmundiff
  • 3,875
  • 7
  • 32
  • 46
  • 2
    Please search before asking next time: http://stackoverflow.com/questions/2080150/when-should-i-use-static-methods-in-a-class-and-what-are-the-benefits http://stackoverflow.com/questions/1299712/why-should-we-use-static-calls-in-php http://stackoverflow.com/questions/1316800/when-to-use-static-modifier-in-php – hobodave Jan 19 '10 at 21:33
  • Your question reads like you're at a similar level of development/knowledge with PHP as myself. Can't recommend 'PHP and MySQL - Beyond the Basic' by Kevin Skoglund - fairly cheap from Lynda.com. Really helped me understand the concepts of public/private/static methods. – suitedupgeek Jan 19 '10 at 21:48

1 Answers1

5

The key concept of using static methods is that they are bound to a class, not an instance of the class. A good guideline is that **anything requiring state is not suitable to being used statically*.

Utility methods are definitely a good candidate for static usage, as they are often short and require no state. Some other guidelines might be:

  • Input and output are not reliant on anything except each other.
  • The method has no context, that is, it doesn't make sense to associate it with an instance of an object.
  • A method/variable requires no differentiation between objects, and a single declaration is all that is required. This applies mostly to static class variables, such as a counter that is shared across all instantiated objects.
zombat
  • 92,731
  • 24
  • 156
  • 164