NYCPHP Meetup

NYPHP.org

[nycphp-talk] Static Methods Usage

Andrew Yochum andrew at plexpod.com
Fri Jan 6 14:47:59 EST 2006


On Fri, Jan 06, 2006 at 02:07:30PM -0500, Cliff Hirsch wrote:
> 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?

If all your methods are static then you're OO code is not too much
different than prcocedural code. So, the answer is to use them when
appropriate.

Here's a basic guideline:
- Use static methods when the code in them does not refer to instance
  data ($this).
- Use regular methods when they need to operate on instance data.

Example:
    class Human {
        private $Name = '';

        public function GetName() {
            return $this->Name;
        }

        public function SetName($Name) {
            $this->Name = $Name;
        }

        public static function GetLatinName() {
            return 'Homo Sapien';
        }
    }

HTH,
Andrew

-- 
Andrew Yochum
Plexpod
andrew at plexpod.com
718-360-0879



More information about the talk mailing list