php 如何从同一个类的静态方法调用非静态方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41631623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:25:46  来源:igfitidea点击:

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

phpoop

提问by Rahul

I am working on PHPcode.

我正在处理PHP代码。

Here is the sample code to explain my problem:

这是解释我的问题的示例代码:

class Foo {

    public function fun1() {
             echo 'non-static';   
    }
    public static function fun2() {
        echo "static" ;
        //self::fun1();
        //Foo::fun1(); 
    }
}

How can I call the non-static method from the static method ?

如何从静态方法调用非静态方法?

Note:Both functions are used throughout the site, which is not known. I can't make any changes in the static/non-static nature of them.

注意:这两个功能在整个站点中都使用,这是未知的。我无法对它们的静态/非静态性质进行任何更改。

回答by Mihai Matei

You must create a new object inside the static method to access non-static methods inside that class:

您必须在静态方法中创建一个新对象才能访问该类中的非静态方法:

class Foo {

    public function fun1()
    {
        return 'non-static';
    }

    public static function fun2()
    {
        return (new self)->fun1();
    }
}

echo Foo::fun2();

The result would be non-static

结果是 non-static

Later edit: As seen an interest in passing variables to the constructor I will post an updated version of the class:

稍后编辑:正如看到将变量传递给构造函数的兴趣,我将发布该类的更新版本:

class Foo {

    private $foo;
    private $bar;

    public function __construct($foo, $bar)
    {
        $this->foo = $foo;
        $this->bar = $bar;
    }

    public function fun1()
    {
        return $this->foo . ' - ' . $this->bar;
    }

    public static function fun2($foo, $bar)
    {
        return (new self($foo, $bar))->fun1();
    }
}

echo Foo::fun2('foo', 'bar');

The result would be foo - bar

结果是 foo - bar

回答by optimoose

The main difference would be that you can call static methods for a class without having to instantiate an object of that class. So, in your static method try

主要区别在于您可以为类调用静态方法,而无需实例化该类的对象。所以,在你的静态方法中尝试

Foo $objInst = new Foo();
$objInst->fun1();

But I don't see how this would make any sense in any context.

但我不明白这在任何情况下都有什么意义。

回答by Fakhar Anwar

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.

Asnwer 选择正确解决问题。有一个有效的用例(设计模式),其中具有静态成员函数的类需要调用非静态成员函数,在此之前,该静态成员还应该使用构造函数和构造函数实例化单例。

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.

案例:例如,我正在实现 Swoole HTTP 请求事件,将其作为具有静态成员的类提供回调。静态成员做两件事;它通过在类构造函数中进行初始化来创建类的单例对象,第二个静态成员所做的是调用非静态方法“run()”来处理请求(通过与 Phalcon 桥接)。因此,没有构造函数和非静态调用的静态类对我不起作用。