When to use self over $this?

前端 未结 23 2833
醉梦人生
醉梦人生 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:19

    $this refers to the current class object, self refers to the current class (Not object). The class is the blueprint of the object. So you define a class, but you construct objects.

    So in other words, use self for static and this for none-static members or methods.

    also in child/parent scenario self / parent is mostly used to identified child and parent class members and methods.

提交回复
热议问题