在 PHP 中,关键字“$this”用作类的自引用,您可以使用它来调用和使用类函数和变量。这是一个例子:
class ClassOne
{
// this is a property of this class
public $propertyOne;
// When the ClassOne is instantiated, the first method called is
// its constructor, which also is a method of the class
public function __construct($argumentOne)
{
// this key word used here to assign
// the argument to the class
$this->propertyOne = $argumentOne;
}
// this is a method of the class
function methodOne()
{
//this keyword also used here to use the value of variable $var1
return 'Method one print value for its '
. ' property $propertyOne: ' . $this->propertyOne;
}
}
当您调用 parent::test() 时,您实际上调用了与 CLASS B 关联的测试函数,因为您是静态调用它。尝试调用它 $this->test() 你应该得到 A 而不是 B。