How to call non-static method from static method of same class?

后端 未结 3 1558
暗喜
暗喜 2021-01-31 07:36

I am working on PHP code.

Here is the sample code to explain my problem:

class Foo {

    public function fun1() {
             echo \'non-static\';   
         


        
3条回答
  •  别那么骄傲
    2021-01-31 08:07

    Asnwer selcted as correct solves problem. There is a valid use case (Design Pattern) where class with static member function needs to call non-static member function and before that this static members should also instantiate singleton using constructor a constructor.

    Case: For example, I am implementing Swoole HTTP Request event providing it a call-back as a Class with static member. Static Member does two things; it creates Singleton Object of the class by doing initialization in class constructor, and second this static members does is to call a non-static method 'run()' to handle Request (by bridging with Phalcon). Hence, static class without constructor and non-static call will not work for me.

提交回复
热议问题