php基礎:類別與物件(5) static_PHP教程

WBOY
發布: 2016-07-21 15:59:22
原創
749 人瀏覽過

Declaring class members or methods as static makes them accessible without needing an instantiation of the class.  class object (though a static method can). 
聲明靜態的類變數和方法可以不需要在實例化類別物件的情況下對他們進行呼叫。靜態類別不能被類別物件呼叫。 (類別的靜態方法可以)。 //注意看第一個例子,在一個非靜態的方法中呼叫了靜態的變數。唯一的不同是用了self。難道用了self就可以????不知道???需要一個試驗。

The static declaration must be after the visibility declaration. For compatibility with PHP4, if. as if it was declared as public. 
靜態聲明必須必須是顯式的聲明。為了兼容PHP4,如果沒有明確聲明的對像或方法,被當作聲明為public。

Because static methods are callable without an instance of the object created, the pseudo variable $.不需要實例化類別物件來呼叫,所以偽變數$this在靜態方法中也是不可用的。

In fact static method calls are resolved at compile time. When using an explicit class name the methodis using an explicit class name the methodis noing an explicit class name the meth 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. 
實際上,靜態的方法呼叫在編譯時已經確定了。 (這段我不會翻譯。???不懂???)
求了很久求來的翻譯如下:
------------------ ------------------------------
實際上,靜態方法的呼叫在編譯時解決。當使用一個明確的類別名稱時,方法已經被完全識別而不需要應用繼承規則。如果由自身調用,那麼自身被解析成當前的類,也就是程式碼所屬的類。這裡也沒有應用繼承規則。
但是一個新的問題:
這裡不一定有繼承產生,為什麼會提到繼承規則?(???不懂????)

Static properties cannot be accessed through the object using the arrow operator ->. Calling non-static methods statically generates an E_STRICT level warning. 
靜態成員無法被類別的物件透過箭頭符號->來呼叫。靜態的呼叫一個非靜態方法會導致一個E_STRICT等級的警告。

靜態成員例:

複製程式碼 程式碼如下:
class Foo 

   public static $my_static = 'foo';  
   public functionsel //注意這裡!!!! 
       //return $my_static;//這樣寫會不會出錯。需要試驗 
   } 


class Bar extends Foo 

  $my_static;//注意這裡!!! ! 
   } 

print Foo::$my_static . " n"; 
$foo = new Foo();
print $foo->my_static . " n";      // 未定義的"Property" my_static  
// $f
$bar = new Bar(); 
print $bar->fooStatic() . " n"; 

() { 
       // ... 
   } 

Foo::



http://www.bkjia.com/PHPjc/317402.html

www.bkjia.com
true

http: //www.bkjia.com/PHPjc/317402.html

Declaringclassmembersormethodsasstaticmakesthemaccessible?
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!