简体   繁体   English

在类中调用静态方法?

[英]call a static method inside a class?

how do i call a static method from another method inside the same class?我如何从同一个类中的另一个方法调用静态方法?

$this->staticMethod();

or或者

$this::staticMethod();

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?这样调用staticMethod()作为实例方法,对吗? It does not.它不是。 This is because the method is declared as public static the interpreter will call it as a static method, so it will work as expected.这是因为该方法被声明为public static解释器将其作为静态方法调用,因此它将按预期工作。 It could be argued that doing so makes it less obvious from the code that a static method call is taking place.可能会争辩说,这样做会使代码中正在发生的静态方法调用变得不那么明显。

$this::staticMethod();

Since PHP 5.3 you can use $var::method() to mean <class-of-$var>:: ;从 PHP 5.3 开始,您可以使用$var::method()来表示<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:因此,这将我们带到了调用静态方法的最常见方式:

self::staticMethod();

Now, before you start thinking that the :: is the static 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;这将打印baz = 1 ,这意味着$this->bar()self::bar()做完全相同的事情; 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;它可以让parent::self::static::工作并让您访问静态变量; how a method is called depends on its signature and how the caller was called.一个方法的调用方式取决于它的签名和调用者的调用方式。

To see all of this in action, see this 3v4l.org output .要查看所有这些操作,请参阅此 3v4l.org 输出

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 self and 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 self refers 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(); 

In the later PHP version self::staticMethod();在以后的 PHP 版本中self::staticMethod(); also will not work.也行不通。 It will throw the strict standard error.它会抛出严格的标准错误。

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();
    }
}

call a static method inside a class在类中调用静态方法

className::staticFunctionName

example例子

ClassName::staticMethod();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM