(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() . "<br/>"; // 出現的是mick
echo $newname->getnamebyself() . "<br/>"; // 出現的是jeff
?>
|
您好: 你結果寫反囉:)
結果反啦!!!
小弟尚菜 看到結果正想這什麼魔術 還好有看到留言 感謝各位先進
反了吧
你害我看好久 XDDDD 我是被誤導的可憐新手
結果反了啦
從別的網站的轉載看到 那邊沒有留言可以看 害我花了一個下午在懷疑人生...