【发布时间】:2014-02-16 09:09:03
【问题描述】:
【问题讨论】:
-
它是变量,
$this总是指不同的对象。他们本可以将其设为像__FILE__这样的magic 常量,但他们没有。嗯,那是 PHP。在许多语言中确实是一样的,其他人使用self,这与其他变量(如foo)无法区分......没有什么必须以某种方式实现,语言设计者可以做随心所欲。
【问题讨论】:
$this总是指不同的对象。他们本可以将其设为像__FILE__ 这样的magic 常量,但他们没有。嗯,那是 PHP。在许多语言中确实是一样的,其他人使用self,这与其他变量(如foo)无法区分......没有什么必须以某种方式实现,语言设计者可以做随心所欲。
The scope of a constant is global。相反,$this 在整个应用程序中发生变化,因为它取决于上下文(即类)。
考虑这个简短的例子:
class A {
function printThis() { echo $this; }
}
class B {
function printThis() { echo $this; }
}
显然,class B 中的$this 与class A 中的$this 不同,因此根据定义它不能是常量*。
*) 编辑: 但是,在 PHP 中存在 magic constants,它会根据上下文而变化:
<?php
$line1 = __LINE__;
$line2 = __LINE__;
assert($line1 == $line2); // fails
所以我认为用户 deceze summarized it pretty well in the comments:“嗯,那是 PHP。”
【讨论】: