php 覆盖类常量与属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13613594/
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
Overriding class constants vs properties
提问by Tom Auger
I would like to better understand why, in the scenario below, there is a difference in the way class constants are inherited vs. instance variables.
我想更好地理解为什么在下面的场景中,类常量的继承方式与实例变量的继承方式有所不同。
<?php
class ParentClass {
const TEST = "ONE";
protected $test = "ONE";
public function showTest(){
echo self::TEST;
echo $this->test;
}
}
class ChildClass extends ParentClass {
const TEST = "TWO";
protected $test = "TWO";
public function myTest(){
echo self::TEST;
echo $this->test;
}
}
$child = new ChildClass();
$child->myTest();
$child->showTest();
Output:
输出:
TWO
TWO
ONE
TWO
In the code above, ChildClass does not have a showTest() method, so the ParentClass showTest() method is used by inheritance. The results show that since the method is executing on the ParentClass, the ParentClass version of the TEST constant is being evaluated, whereas because it's evaluating within the ChildClass context via inheritance, the ChildClass member variable $test is being evaluated.
上面代码中,ChildClass没有showTest()方法,所以继承使用了ParentClass的showTest()方法。结果表明,由于该方法是在 ParentClass 上执行的,因此正在评估 TEST 常量的 ParentClass 版本,而由于它是通过继承在 ChildClass 上下文中进行评估的,因此正在评估 ChildClass 成员变量 $test。
I've read the documentation, but can't seem to see any mention of this nuance. Can anyone shed some light for me?
我已阅读文档,但似乎看不到任何提及此细微差别的内容。任何人都可以为我提供一些帮助吗?
回答by David Farrell
self::Isn't inheritance-aware and always refers to the class it is being executed in. If you are using php5.3+ you might try static::TESTas static::is inheritance-aware.
self::是不是继承意识,并始终指向它正在被执行的类。如果你正在使用PHP5.3 +你可以尝试static::TEST的static::是继承意识。
The difference is that static::uses "late static binding". Find more information here:
不同之处在于static::使用“后期静态绑定”。在此处查找更多信息:
http://php.net/manual/en/language.oop5.late-static-bindings.php
http://php.net/manual/en/language.oop5.late-static-bindings.php
Here's a simple test script I wrote:
这是我写的一个简单的测试脚本:
<?php
class One
{
const TEST = "test1";
function test() { echo static::TEST; }
}
class Two extends One
{
const TEST = "test2";
}
$c = new Two();
$c->test();
output
输出
test2
回答by w00
In PHP, self refers to the class in which the called method or property is defined. So in your case you're calling selfin ChildClass, so it uses the variable from that class. Then you use selfin ParentClass, so it wil then refer to the variable in that class.
在 PHP 中,self 指的是定义被调用方法或属性的类。所以你的情况你打电话self的ChildClass,所以它使用从该类变量。然后您使用selfin ParentClass,因此它将引用该类中的变量。
if you still want the child class to override the constof the parent class, then adjust the following code in your parent class to this:
如果您仍然希望子类覆盖const父类的 ,则将父类中的以下代码调整为:
public function showTest(){
echo static::TEST;
echo $this->test;
}
Note the statickeyword. This is uses "late static binding". Now you're parent class will call the const of your child class.
注意static关键字。这是使用“后期静态绑定”。现在您的父类将调用您子类的 const。

