php基礎(chǔ)知識(shí):類(lèi)與對(duì)象(5) static
Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can).
聲明靜態(tài)的類(lèi)變量和方法可以不需要實(shí)例化類(lèi)對(duì)象的情況下對(duì)他們進(jìn)行調(diào)用。靜態(tài)類(lèi)不能被類(lèi)對(duì)象調(diào)用。(類(lèi)的靜態(tài)方法可以)。//注意看第一個(gè)例子,在一個(gè)非靜態(tài)的方法中調(diào)用了靜態(tài)的變量。唯一的不同是用了self。難道用了self就可以????不知道???需要一個(gè)試驗(yàn)。
The static declaration must be after the visibility declaration. For compatibility with PHP4, if no visibility declaration is used, then the member or method will be treated as if it was declared as public.
靜態(tài)聲明必須必須是顯式的聲明。為了兼容PHP4,如果沒(méi)有顯式聲明的對(duì)象或者方法,被當(dāng)作聲明為public。
Because static methods are callable without an instance of the object created, the pseudo variable $this is not available inside the method declared as static.
因?yàn)殪o態(tài)方法不需要實(shí)例化類(lèi)對(duì)象來(lái)調(diào)用,所以偽變量$this在靜態(tài)方法中也是不可用的。
In fact static method calls are resolved at compile time. When using an explicit class name the method is already identified completely and no inheritance rules apply. If the call is done by self then self is translated to the current class, that is the class the code belongs to. Here also no inheritance rules apply.
實(shí)際上,靜態(tài)的方法調(diào)用在編譯時(shí)已經(jīng)確定了。(這段我不會(huì)翻譯。???不明白???)
求了很久求來(lái)的翻譯如下:
------------------------------------------------
實(shí)際上,靜態(tài)方法的調(diào)用在編譯時(shí)解決。當(dāng)使用一個(gè)明確的類(lèi)名時(shí),方法已經(jīng)被完全識(shí)別而不需要應(yīng)用繼承規(guī)則。如果由自身調(diào)用,那么自身被解析成當(dāng)前的類(lèi),也就是代碼所屬的類(lèi)。這里也沒(méi)有應(yīng)用繼承規(guī)則。
但是一個(gè)新的問(wèn)題:
這里不一定有繼承產(chǎn)生,為什么會(huì)提到繼承規(guī)則?(???不明白????)
Static properties cannot be accessed through the object using the arrow operator ->. Calling non-static methods statically generates an E_STRICT level warning.
靜態(tài)成員不能被類(lèi)的對(duì)象通過(guò)箭頭符號(hào)->來(lái)調(diào)用。靜態(tài)的調(diào)用一個(gè)非靜態(tài)方法會(huì)導(dǎo)致一個(gè)E_STRICT級(jí)別的警告。
靜態(tài)成員例:
{
public static $my_static = 'foo';
public function staticValue() {
return self::$my_static;//注意這里!!!!
//return $my_static;//這樣寫(xiě)會(huì)不會(huì)出錯(cuò)。需要試驗(yàn)
}
}
class Bar extends Foo
{
public function fooStatic() {
return parent::$my_static;//注意這里!!!!
}
}
print Foo::$my_static . " n";
$foo = new Foo();
print $foo->staticValue() . " n";
print $foo->my_static . " n"; // 未定義的"Property" my_static
// $foo::my_static is not possible
print Bar::$my_static . " n";
$bar = new Bar();
print $bar->fooStatic() . " n";
靜態(tài)方法例:
class Foo {
public static function aStaticMethod() {
// ...
}
}
Foo::aStaticMethod();
- PHP類(lèi)與對(duì)象后期靜態(tài)綁定操作實(shí)例詳解
- 詳解php中的類(lèi)與對(duì)象(繼承)
- PHP類(lèi)與對(duì)象中的private訪問(wèn)控制的疑問(wèn)
- php基礎(chǔ)知識(shí):類(lèi)與對(duì)象(4) 范圍解析操作符(::)
- php基礎(chǔ)知識(shí):類(lèi)與對(duì)象(3) 構(gòu)造函數(shù)和析構(gòu)函數(shù)
- php基礎(chǔ)知識(shí):類(lèi)與對(duì)象(2) 自動(dòng)加載對(duì)象
- php基礎(chǔ)知識(shí):類(lèi)與對(duì)象(1)
- PHP學(xué)習(xí)記錄之面向?qū)ο螅∣bject-oriented programming,OOP)基礎(chǔ)【接口、抽象類(lèi)、靜態(tài)方法等】
- PHP學(xué)習(xí)記錄之面向?qū)ο螅∣bject-oriented programming,OOP)基礎(chǔ)【類(lèi)、對(duì)象、繼承等】
- PHP面向?qū)ο蟪绦蛟O(shè)計(jì)子類(lèi)擴(kuò)展父類(lèi)(子類(lèi)重新載入父類(lèi))操作詳解
- PHP中類(lèi)與對(duì)象功能、用法實(shí)例解讀
相關(guān)文章
php實(shí)現(xiàn)將wav文件轉(zhuǎn)換成圖像文件并在頁(yè)面中顯示的方法
這篇文章主要介紹了php實(shí)現(xiàn)將wav文件轉(zhuǎn)換成圖像文件并在頁(yè)面中顯示的方法,涉及php中unpack、fopen、fread等方法及圖形操作的相關(guān)技巧,需要的朋友可以參考下2015-04-04php抽獎(jiǎng)小程序的實(shí)現(xiàn)代碼
本篇文章是對(duì)php實(shí)現(xiàn)抽獎(jiǎng)的程序代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06解析左右值無(wú)限分類(lèi)的實(shí)現(xiàn)算法
本篇文章是對(duì)php左右值無(wú)限分類(lèi)的實(shí)現(xiàn)算法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP實(shí)現(xiàn)的只保留字符串首尾字符功能示例【隱藏部分字符串】
這篇文章主要介紹了PHP實(shí)現(xiàn)的只保留字符串首尾字符功能,結(jié)合實(shí)例形式分析了php隱藏部分字符串相關(guān)的字符串遍歷、截取相關(guān)操作技巧,需要的朋友可以參考下2019-03-03微信公眾平臺(tái)之快遞查詢(xún)功能用法實(shí)例
這篇文章主要介紹了微信公眾平臺(tái)之快遞查詢(xún)功能用法,實(shí)例分析了微信公眾平臺(tái)實(shí)現(xiàn)快遞查詢(xún)的相關(guān)技巧與具體用法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04Discuz 5.0 中讀取純真IP數(shù)據(jù)庫(kù)函數(shù)分析
Discuz 5.0 中讀取純真IP數(shù)據(jù)庫(kù)函數(shù)分析...2007-03-03