class?Car?{
????private?$ary?=?array();
????
????public?function?__set($key,?$val)?{
????????$this->ary[$key]?=?$val;
????}
????
????public?function?__get($key)?{
????????if?(isset($this->ary[$key]))?{
????????????return?$this->ary[$key];
????????}
????????return?null;
????}
????
????public?function?__isset($key)?{
????????if?(isset($this->ary[$key]))?{
????????????return?true;
????????}
????????return?false;
????}
????
????public?function?__unset($key)?{
????????unset($this->ary[$key]);
????}
}
$car?=?new?Car();
$car->name?=?'汽車(chē)';??//name屬性動(dòng)態(tài)創(chuàng)建并賦值
echo?$car->name;
2019-01-03
2018-07-27
動(dòng)態(tài)產(chǎn)生的對(duì)象屬性是全局性的,這與變量無(wú)需聲明就可訪問(wèn)是一脈相承的
但是由于一些學(xué)究的批評(píng),php?開(kāi)始逐漸放棄這種輕巧的表述方法了
所以,應(yīng)該盡量不要這樣做
聽(tīng)大佬們說(shuō)的