When to use self over $this?

前端 未结 23 2832
醉梦人生
醉梦人生 2020-11-21 11:19

In PHP 5, what is the difference between using self and $this?

When is each appropriate?

23条回答
  •  别跟我提以往
    2020-11-21 12:03

    Inside a class definition, $this refers to the current object, while self refers to the current class.

    It is necessary to refer to a class element using self, and refer to an object element using $this.

    self::STAT // refer to a constant value
    self::$stat // static variable
    $this->stat // refer to an object variable  
    

提交回复
热议问题