上圖概要的說了下php類的特性,類的方法同屬性類似。
圖中B類繼承自A類,B是A的子類,$x和$y都是B的實例化對象。
1. 原型引用:【A:: 、 B:: 】,僅限public static 屬性和方法
2. 實例引用:【$x-> 、$y-> 】,僅限public 屬性和public方法以及public static 方法
3. 關於 self:: 和 parent:: (類引用)
self:: 當前方法所屬的類
parent:: 父類
如果self::后面的屬性或者方法未在當前類中定義,會嘗試用parent::替代self::
self:: 和 parent:: 后面只能跟方法名或靜態屬性
4. 關於 $this (對象引用)
$x->f() 中的 $this 是$x。
$y->f() 中的 $this 是$y。
$this-> 后面可以是動態屬性以及動態或靜態方法。
5. 關於 static::
static:: 引用當前使用的類,類似 $this,但可以在未實例化的類中使用,在已實例化對象中可以理解成等同於$this
6. 關於static
static標識的屬性只能通過 self:: 、parent::、static:: 、類名:: 靜態引用,不能通過 $this-> 對象引用
6. 關於 public 、protected 、private
public : 可以在任何地方引用
protected : 只能在類中引用,$this-> 、parent::
private : 只能在本類中引用, $this->、self::