(1).self是参照到当前的class,$this是参照到当前的object ( 已经被声明的实体上 )
(2).self 可使用在static上,$this不行
static method 因为没有对应的实体,所以需要注意不可以使用 $this ,要用self::
可以直接存取 static method ( 如self::method() ),但是无法直接存取 static property 中的预先声明的值
(3). 可用 new self() 调用自己
以下是(1)的范例:
class name
{
public $name;
public function getname()
{
return $this->name = "mick";
}
public function getnamebythis()
{
return $this->getname();
}
public function getnamebyself()
{
return self::getname();
}
}
class name2 extends name
{
public function getname()
{
return $this->name = "jeff";
}
}
$newname = new name2();
echo $newname->getnamebythis() . "
"; // 出现mick
echo $newname->getnamebyself() . "
"; // 出现jeff