首页 >社区问答列表 >PHP类中的$this

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代表的是整个类?

  • yntdx
  • yntdx    2021-10-12 13:44:402楼

    this对象是必须是用 new操作符分配的(而不是用new[],也不是用placement new,也不是局部对象,也不是global对象);delete this后,不能访问该对象任何的成员变量及虚函数(delete this回收的是数据,这包括对象的数据成员以及vtable,不包括函数代码);delete this后,不能再访问this指针。换句话说,你不能去检查它、将它和其他指针比较、和 NULL比较、打印它、转换它,以及其它的任何事情

    +0添加回复

  • 回复