是否可以在PHP中動態(tài)定義類屬性值?是否可以定義PHP類屬性并使用同一類中的屬性動態(tài)分配值?就像是:class user {
public $firstname = "jing";
public $lastname = "ping";
public $balance = 10;
public $newCredit = 5;
public $fullname = $this->firstname.' '.$this->lastname;
public $totalBal = $this->balance+$this->newCredit;
function login() {
//some method goes here!
}}產(chǎn)量:解析錯誤:語法錯誤,第6行意外的'$ this'(T_VARIABLE)上面的代碼有什么問題嗎?如果是這樣,請指導我,如果不可能,那么實現(xiàn)這一目標的好方法是什么?
2 回答

浮云間
TA貢獻1829條經(jīng)驗 獲得超4個贊
不,你不能設置這樣的屬性。
但是:您可以在構(gòu)造函數(shù)中設置它們,因此如果有人創(chuàng)建了該類的實例,它們將可用:
public function __construct(){ $this->fullname = $this->firstname . ' ' . $this->lastname;}
- 2 回答
- 0 關注
- 925 瀏覽
添加回答
舉報
0/150
提交
取消