Is static method faster than non-static?

You should always use static when you don't need an object around you method, and use dynamic when you need an object. In the example you provides, you don't need an object, because the method doesn't interact with any properties or fields in your class.

This one should be static, because it doesn't need an object:

class Person {
    public static function GetPersonByID($id) {
        //run SQL query here
        $res = new Person();
        $res->name = $sql["name"];
        //fill in the object
        return $res;
    }
}

This one should be dynamic, because it uses the object it is in:

class Person {
    public $Name;
    public $Age;
    public function HaveBirthday() {
        $Age++;
    }
}

The speed diffirence is minimal, but you have to create an object to run dynamic methods, and that object is saved in memory, so dynamic methods use more memory and a little more time.


Short Answer since I don't want to go on a rant to much:

It doesn't matter if it's faster. If you need something where performance is THAT important that you think about shaving of 0.02 nanoseconds per function call than you're not going to do it in PHP anyways.

Static methods make for untestable, unmaintainable, "global everything" code that is going to hurt you much more than anything else.

If you don't want to use proper OOP (and thats totally fine if you know what and why you are doing it) then please do so. Just don't do it because you want to save cpu time.

http://misko.hevery.com/2008/12/15/static-methods-are-death-to-testability/

http://sebastian-bergmann.de/archives/885-Stubbing-Hard-Coded-Dependencies.html

http://en.wikipedia.org/wiki/Class-based_programming

If you only click one link: http://www.scribd.com/doc/20893084/Advanced-OOP-and-Design-Patterns#scribd

Premature optimization is the root of all evil. Build code that is easy to maintain and if it's getting slow take a profile and it will most likely tell you that the filesystem oder the database is problem, one you got all that sorted out there will be some very specific pieces of php to optimize. If you got clean, changeable code you can speed those up.


"Premature optimization is the root of all evil" was said 40 years ago by Donald Knuth. You know, back when you had the new 4004 microprocessor that Intel invented. That drum is beat as hard as any horse can be and I fail to see how it relates to the original question. In fact, I may have been lucky, but I haven't seen evidence of this rampant behavior in the field. Alas, someone on the internet has to be right before we can all tuck in for the night.

More on topic, I think the accepted answer is the most pragmatic and the first comment to the question is the right one to ask. Whether static vs. an instantiated code is faster is mostly dependent on the way the language is implemented and I don't see that any of these responses sufficiently answer the question in regards to PHP. Anyone know PHP and want to weigh in?


Generally, yes. Static methods and properties work faster (except in PHP 5.3).

You can refer to this more or less detailed comparison of static and non-static methods in PHP.