PHP 中的 self::$bar 和 static::$bar 有什么区别?

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

What is the difference between self::$bar and static::$bar in PHP?

phpoopphp-5.3

提问by cwd

What is the difference between using selfand staticin the example below?

在下面的示例中使用self和 有什么区别static

class Foo
{
    protected static $bar = 1234;

    public static function instance()
    {
        echo self::$bar;
        echo "\n";
        echo static::$bar;
    }

}

Foo::instance();

produces

产生

1234
1234

回答by BoltClock

When you use selfto refer to a class member, you're referring to the class within which you use the keyword. In this case, your Fooclass defines a protected static property called $bar. When you use selfin the Fooclass to refer to the property, you're referencing the same class.

当您self用来引用类成员时,您指的是在其中使用关键字的类。在这种情况下,您的Foo类定义了一个受保护的静态属性,称为$bar。当您selfFoo类中使用来引用属性时,您引用的是同一个类。

Therefore if you tried to use self::$barelsewhere in your Fooclass but you had a Barclass with a different value for the property, it would use Foo::$barinstead of Bar::$bar, which may not be what you intend:

因此,如果您尝试self::$barFoo类中的其他地方使用,但您有一个Bar具有不同属性值的类,它将使用Foo::$bar代替Bar::$bar,这可能不是您想要的:

class Foo
{
    protected static $bar = 1234;
}

class Bar extends Foo
{
    protected static $bar = 4321;
}

When you calla method via static, you're invoking a feature called late static bindings(introduced in PHP 5.3).

当您通过调用方法时static,您正在调用一个称为后期静态绑定的功能(在 PHP 5.3 中引入)。

In the above scenario, using selfwill result in Foo::$bar(1234). And using staticwill result in Bar::$bar(4321) because with static, the interpreter takes takes into account the redeclaration within the Barclass during runtime.

在上述场景中,使用self将导致Foo::$bar(1234)。并且 usingstatic将导致Bar::$bar(4321) 因为 with static,解释器会Bar在运行时考虑类中的重新声明。

You typically use late static bindings for methods or even the class itself, rather than properties, as you don't often redeclare properties in subclasses; an example of using the statickeyword for invoking a late-bound constructor can be found in this related question: New self vs. new static

您通常对方法甚至类本身使用后期静态绑定,而不是属性,因为您不经常在子类中重新声明属性;static可以在此相关问题中找到使用关键字调用后期绑定构造函数的示例:New self vs. new static

However, that doesn't preclude using staticwith properties as well.

但是,这也不排除使用staticwith 属性。

回答by ggedde

As mentioned one of the main differences is that staticallows for late static bindings. One of the most useful scenarios that I found was for creating Base classes for Singleton Classes:

如前所述,主要区别之一是static允许后期静态绑定。我发现的最有用的场景之一是为单例类创建基类:

class A { // Base Class
    protected static $name = '';
    protected static function getName() {
        return static::$name;
    }
}
class B extends A {
    protected static $name = 'MyCustomNameB';
}
class C extends A {
    protected static $name = 'MyCustomNameC';
}

echo B::getName(); // MyCustomNameB
echo C::getName(); // MyCustomNameC

Using return static::$namein the Base class will return what was statically attached when it was extended. If you were to use return self::$namethen B::getName()would return an empty string as that is what is declared in the Base class.

使用return static::$nameBase类将返回时,又延长了什么静态连接。如果您要使用return self::$namethenB::getName()将返回一个空字符串,因为这是在 Base 类中声明的内容。