我们要实现的是继承独立于其他类的ChildrenA :: $ property。如果在类的默认值中声明ChildrenA :: $ property,它将变得独立。
//# ============ Declare class
Class ParentClass{
public static $property = ["parent"=>"im Parent"];
}
Class ChildrenA extends ParentClass {
//# must put in init because in real case the value is an expression
public static function init(){
static::$property["childrenA"] = "Im Children A";
}
}
Class ChildrenB extends ParentClass {
}
//# =============================================
//# in real case this called in app singleton
ChildrenA::init();
var_dump(ChildrenB::$property);
var_dump(ParentClass::$property);
var_dump结果:ParentClass和ChildrenB也具有ChildrenA静态属性
array(2) {
'parent' =>
string(9) "im Parent"
'childrenA' =>
string(13) "Im Children A"
}
如何让孩子独立A