Performance of Singleton Class Instance Method vs. Static Class Method in PHP?

前端 未结 5 1504
天涯浪人
天涯浪人 2021-02-06 12:06

I\'m interested in objective analysis of which is more performant; calling instance methods of a singleton class or methods of a static class. I\'ve already seen this so I\'m no

5条回答
  •  执念已碎
    2021-02-06 12:54

    Before you can call the instance method of a singleton pattern object, you need to get the instance first, which requires a static method call:

    SomeClass::getInstance()->myMethod();
    // versus
    SomeClass::myMethod();
    

    So the first time you need access to that object in a function, you need to make a static method call first. Because function calls are never free, you are probably better off making the method static.

提交回复
热议问题