在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 ::測試(),你實際調用,因爲你是靜態調用它與B類相關的測試功能。嘗試稱它$ this-> test(),你應該得到A不是B.
首先,我要正確地命令這些類(在這樣做後它會更有意義)。 '$ this'的值是類'A'。 – Christian