php self,$this,const,static,->的使用
更新時間:2009年10月22日 13:18:51 作者:
用php這么久了,慚愧的是,原來自己還一直沒分清楚這幾個關鍵字使用方法。
今天來總結(jié)下。
、在類的內(nèi)部方法訪問已經(jīng)聲明為const及static的屬性時,使用self::$name的形式。注意的是const屬性的申明格式,const PI=3.14,而不是const $PI=3.14
class clss_a {
private static $name="static class_a";
const PI=3.14;
public $value;
public static function getName()
{
return self::$name;
}
//這種寫法有誤,靜態(tài)方法不能訪問非靜態(tài)屬性
public static function getName2()
{
return self::$value;
}
public function getPI()
{
return self::PI;
}
}
還要注意的一點是如果類的方法是static的,他所訪問的屬性也必須是static的。
、在類的內(nèi)部方法訪問未聲明為const及static的屬性時,使用$this->value ='class_a';的形式。
、在類的內(nèi)部方法訪問已經(jīng)聲明為const及static的屬性時,使用self::$name的形式。注意的是const屬性的申明格式,const PI=3.14,而不是const $PI=3.14
復制代碼 代碼如下:
class clss_a {
private static $name="static class_a";
const PI=3.14;
public $value;
public static function getName()
{
return self::$name;
}
//這種寫法有誤,靜態(tài)方法不能訪問非靜態(tài)屬性
public static function getName2()
{
return self::$value;
}
public function getPI()
{
return self::PI;
}
}
還要注意的一點是如果類的方法是static的,他所訪問的屬性也必須是static的。
、在類的內(nèi)部方法訪問未聲明為const及static的屬性時,使用$this->value ='class_a';的形式。
相關文章
PHP中define() 與 const定義常量的區(qū)別詳解
這篇文章主要介紹了PHP中define() 與 const定義常量的區(qū)別,結(jié)合實例形式分析了php中使用define()與const定義常量的具體使用原理、技巧與相關用法區(qū)別,需要的朋友可以參考下2019-06-06淺析Dos下運行php.exe,出現(xiàn)沒有找到php_mbstring.dll 錯誤的解決方法
本篇文章是對在Dos下運行php.exe,出現(xiàn)沒有找到php_mbstring.dll 錯誤的解決方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06PHP+MySQL+sphinx+scws實現(xiàn)全文檢索功能詳解
這篇文章主要介紹了PHP+MySQL+sphinx+scws實現(xiàn)全文檢索功能,詳細分析了sphinx、scws的下載安裝、配置、數(shù)據(jù)庫創(chuàng)建及全文檢索功能的相關實現(xiàn)技巧,需要的朋友可以參考下2019-08-08