[nycphp-talk] Static Methods Usage
Hans Zaunere
lists at zaunere.com
Mon Jan 9 23:35:38 EST 2006
Cliff Hirsch wrote on Friday, January 06, 2006 2:08 PM:
> As I refactor my code yet again, I am torn about whether to make
> wide-spread use of static methods (in PHP 5) when appropriate.
>
> The manual says:
>
> "Declaring class members or methods as static makes them accessible
> without needing an instantiation of the class....In fact static method
> calls are resolved at compile time."
>
> I presume this makes static method calls faster. But by enough to make
> it worthwhile? Thoughts? Opinions? Suggestions?
As others have mentioned, I wouldn't consider the use of static methods
simply because of performance. If performance is such a concern, then pure
procedural code - and no OO - will beat out static methods.
Instead, static methods should be considered conceptually. They provide
functionality that can be executed without an object. Said another way,
they provide functionality that is object agnostic. In other words, they
are class specific, rather than object specific.
This is particularly relevant when doing operations that aren't associated
with a particular class instance - or object. A good example is the
singleton pattern. Static methods are a must in this case, since you want
to be able to work with a class without concern for a particular instance of
that class, ie, "global" use of a class, rather than a "local" instance of
that class, or in other words, an object.
It's akin to static variables in a function - regardless of the function
call, you need a variable to remain with the same data - static methods and
properties provide a way to access class functionality and data, regardless
of the particular object that's in use.
---
Hans Zaunere / President / New York PHP
www.nyphp.org / www.nyphp.com
More information about the talk
mailing list