PHPにおけるstaticキーワードおよびselfキーワードとの違い

2522 ワード

概要
デザインモードを勉強していて、前に一例モードについての文章がありましたが、この文章を読み直してみると、staticキーワードの習得がしっかりしていないことに気づき、もう一度復習しました.
staticキーワード
PHPマニュアルのstaticキーワードの紹介は以下の通りです.
 
  
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static cannot be accessed with an instantiated class object (though a static method can).

クラスのプロパティとメソッドを静的と宣言すると、インスタンス化オブジェクトを必要とせずに静的プロパティとメソッドに直接アクセスできます.
PHPの静的メンバーとメソッドの特性は次のとおりです.
1.静的メンバーはクラスのインスタンスからアクセスできませんが、静的メソッドは可能です.2.静的メンバーは->演算子からアクセスできません.3.静的メソッドの役割ドメインでは、$thisキーワードは表示されません.つまり、静的メソッドでは通常のメンバー変数にアクセスできません.4.オブジェクトをインスタンス化することなく、静的メンバーとメソッドはクラス名で直接アクセスできます.
遅延バインド(Late Static Bindings)
以下の内容はPHPマニュアルから抜粋します.
 
  
PHP 5.3.0 ,PHP , 。
, “ ”(non-forwarding call) 。 , ( :: ); , 。 “ ”(forwarding call) :self::,parent::,static:: forward_static_call()。 get_called_class() ,static:: 。

この特性の理解については、以下のマニュアルの例を参照してください.
self vs static
selfとstaticの違いを1つのdemoで直接説明します.selfの例:
 
  
class Vehicle {
    protected static $name = 'This is a Vehicle';
    public static function what_vehicle() {
        echo get_called_class()."
";               
        echo self::$name;
    }
}
class Sedan extends Vehicle {
    protected static $name = 'This is a Sedan';
}
Sedan::what_vehicle();

プログラム出力:
 
  
Sedan
This is a Vehicle

staticの例:
 
  
class Vehicle {
    protected static $name = 'This is a Vehicle';
    public static function what_vehicle() {
        echo get_called_class()."
";       
        echo static::$name;
    }
}
class Sedan extends Vehicle {
    protected static $name = 'This is a Sedan';
}
Sedan::what_vehicle();

プログラム出力:
 
  
Sedan
This is a Sedan

まとめ
前の文章を见て、すでに1ヶ月以上ブログを更新したことがなくて、忙しくて一部で、主なのはやはり自分で怠って、后でまた坚持しなければなりません.この文章も少し感じがしない.