7

I recently answered this question:

What are good reasons to use static methods in PHP?

The first thing to come to mind, of course, was a singleton. With little exception, the other answerers provided the same singleton example. But that got me thinking... I don't really ever use static methods or properties for anything besides creating singletons!

A brief search netted many tutorials on using static methods, almost all of which implement some variation of the same singleton class.

I'm really interested: What reason do we have to make static methods other than creating singletons (or other than just being lazy and wanting a global function)?

Does anyone have a pragmatic example of using a static method that could not be accomplished better using a dynamic design pattern? The example can be a singleton if that makes sense in it's context, but I'm interested in other reasons besides the singleton-aspect of the solution.

3 Answers 3

5

A factory pattern will normally use a static call as well; but it's sensible to use static methods for any class mathod that has no dependencies on the instance properties or other instance methods, especially when they're called regularly, for performance reasons.

The logical place for the following method in PHPExcel is in the PHPExcel_Cell class, because it pertains directly to manipulating a cell address (any cell address, not just the address of a specific instance), but it has no dependency on the instance, so I declare it static.

public static function stringFromColumnIndex($pColumnIndex = 0) { if ($pColumnIndex < 26) { return chr(65 + $pColumnIndex); } elseif ($pColumnIndex < 702) { return chr(64 + ($pColumnIndex / 26)).chr(65 + $pColumnIndex % 26); } return chr(64 + (($pColumnIndex - 26) / 676)).chr(65 + ((($pColumnIndex - 26) % 676) / 26)).chr(65 + $pColumnIndex % 26); } 

And this method isn't particularly difficult to test

Sign up to request clarification or add additional context in comments.

8 Comments

And how exactly do I mock the static function call for this when I'm testing another object that uses this?
Oh! I'd forgotten that there was only one true test methodology
@Mark James raises a valid question. And I'd say it's interesting for anyone who wants to use PHPExcel in their own projects to know how it's creators suggest to test it.
@Gordon - I can't use PHPUnit or SimpleTest for module testing, for reasons other than static methods/properties anyway... I've yet to figure out how to mock up a nested tree of self-referential (in both directions) objects. All my unit tests are hand-crafted, run via phpt, and the tests are ordered so statics are tested before anything that might reference them. Probably not the answer you're looking for: but when testing PHPExcel itself, it's easiest for users to treat it as a "black box" in much the same was as (for example) PDO is tested... after all, how do you mock a result set in PDO?
@Gordon - Any further discussion is probably better continuing elsewhere. I think you and I disagree about statics, and about certain design patterns, and that's probably not appropriate for this forum.
|
4

It's not necessarily about patterns. PHP is not a persistent environment, so lifetime of any object instance is likely to be milliseconds, but it requires memory allocation/release and extra cpu cycles. Unless you need reusable objects or collections, I find non-static objects to have little justification.

3 Comments

Sorry, you can't, because I don't use any. Still, none of over 50 client systems installed in the last two years experience any problems :)
there's always place for irony and... um... supercoders, ain't it :) this is getting off topic, i'm out.
+1 for indicating the fact that PHP isn't persistent. All this could really be for naught.
2

Tongue in cheek answer, there aren't any. Static methods are very very hard to test or mock.

4 Comments

+1 and the same is true for Singletons. Avoid both althogether.
Singletonitis - the plague of iffy oop designs
Public static methods that reference global state (class public static state is global state too) can cause great difficulties in testing, especially negative tests for changes in global state. Static methods (or global functions) which only operate on arguments passed to them and call no other (or only known trusted) functions are extremely easy to test - no mocking needed.
-1 Nice links but they all pinpoint the underlying issue. Don't couple static methods and (especially) global state. A stateless static method is actually easier to test because you don't need to waste time/memory on unnecessary object constructions. All it requires is one simple rule, "respect the local scope". Developers who don't understand what that means shouldn't use static methods. Static methods aren't some magical construct made of unicorn farts and fairy ashes. They're just namespaced functions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.