PHP類中的$this
問題描述
class DBmodel{ private $name; public __construct(){ $this->name = $name; } public function delete($name){$this->name = $name; }}
__construct里面的$this就代表__construct本身嗎?delete里面的$this就代表delete本身嗎?還是$this代表的是整個(gè)類?
問題解答
回答1:$this代表的是整個(gè)類
回答2:this對(duì)象是必須是用 new操作符分配的(而不是用new[],也不是用placement new,也不是局部對(duì)象,也不是global對(duì)象);delete this后,不能訪問該對(duì)象任何的成員變量及虛函數(shù)(delete this回收的是數(shù)據(jù),這包括對(duì)象的數(shù)據(jù)成員以及vtable,不包括函數(shù)代碼);delete this后,不能再訪問this指針。換句話說(shuō),你不能去檢查它、將它和其他指針比較、和 NULL比較、打印它、轉(zhuǎn)換它,以及其它的任何事情
回答3:$this代表的是對(duì)象,而$this所在環(huán)境為類內(nèi)部的方法內(nèi)部,所以$this對(duì)象是在類內(nèi)部訪問
相關(guān)文章:
