php 在类中调用静态方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2204018/
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-25 05:30:20  来源:igfitidea点击:

call a static method inside a class?

php

提问by ajsie

how do i call a static method from another method inside the same class?

我如何从同一个类中的另一个方法调用静态方法?

$this->staticMethod();

or

或者

$this::staticMethod();

回答by Ja?ck

Let's assume this is your class:

让我们假设这是你的班级:

class Test
{
    private $baz = 1;

    public function foo() { ... }

    public function bar() 
    {
        printf("baz = %d\n", $this->baz);
    }

    public static function staticMethod() { echo "static method\n"; }
}

From within the foo()method, let's look at the different options:

foo()方法内部,让我们看看不同的选项:

$this->staticMethod();

So that calls staticMethod()as an instance method, right? It does not. This is because the method is declared as public staticthe interpreter will call it as a static method, so it will work as expected. It could be argued that doing so makes it less obvious from the code that a static method call is taking place.

所以它staticMethod()作为一个实例方法调用,对吗?它不是。这是因为该方法被声明为public static解释器将其作为静态方法调用,因此它将按预期工作。可能会争辩说,这样做会使代码中正在发生的静态方法调用变得不那么明显。

$this::staticMethod();

Since PHP 5.3 you can use $var::method()to mean <class-of-$var>::; this is quite convenient, though the above use-case is still quite unconventional. So that brings us to the most common way of calling a static method:

由于PHP 5.3,你可以使用$var::method()的意思<class-of-$var>::; 这很方便,尽管上述用例仍然非常规。因此,这将我们带到了调用静态方法的最常见方式:

self::staticMethod();

Now, before you start thinking that the ::is thestatic call operator, let me give you another example:

现在,你开始思考的是,之前::静态调用操作,让我给你举个例子:

self::bar();

This will print baz = 1, which means that $this->bar()and self::bar()do exactly the same thing; that's because ::is just a scope resolution operator. It's there to make parent::, self::and static::work and give you access to static variables; how a method is called depends on its signature and how the caller was called.

这将 print baz = 1,这意味着$this->bar()self::bar()做完全相同的事情;那是因为::它只是一个范围解析运算符。它的存在使parent::self::static::工作,给你访问静态变量; 一个方法的调用方式取决于它的签名和调用者的调用方式。

To see all of this in action, see this 3v4l.org output.

要查看所有这些操作,请参阅此 3v4l.org 输出

回答by Joundill

This is a very late response, but adds some detail on the previous answers

这是一个很晚的回复,但在以前的答案中添加了一些细节

When it comes to calling static methods in PHP from another static method on the same class, it is important to differentiate between selfand the class name.

当涉及从同一类上的另一个静态方法调用 PHP 中的静态方法时,区分self和 类名很重要。

Take for instance this code:

以这段代码为例:

class static_test_class {
    public static function test() {
        echo "Original class\n";
    }

    public static function run($use_self) {
        if($use_self) {
            self::test();
        } else {
            $class = get_called_class();
            $class::test(); 
        }
    }
}

class extended_static_test_class extends static_test_class {
    public static function test() {
        echo "Extended class\n";
    }
}

extended_static_test_class::run(true);
extended_static_test_class::run(false);

The output of this code is:

这段代码的输出是:

Original class

Extended class

原班

扩展类

This is because selfrefers to the class the code is in, rather than the class of the code it is being called from.

这是因为self指的是代码所在的类,而不是调用它的代码的类。

If you want to use a method defined on a class which inherits the original class, you need to use something like:

如果要使用在继承原始类的类上定义的方法,则需要使用以下内容:

$class = get_called_class();
$class::function_name(); 

回答by Nishad Up

In the later PHP version self::staticMethod();also will not work. It will throw the strict standard error.

在以后的 PHP 版本中self::staticMethod();也行不通。它会抛出严格的标准错误。

In this case, we can create object of same class and call by object

在这种情况下,我们可以创建同一个类的对象并按对象调用

here is the example

这是例子

class Foo {

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

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