When to use self over $this?

前端 未结 23 2808
醉梦人生
醉梦人生 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 11:59

    I ran into the same question and the simple answer is:

    • $this requires an instance of the class
    • self:: doesn't

    Whenever you are using static methods or static attributes and want to call them without having an object of the class instantiated you need to use self: to call them, because $this always requires on object to be created.

提交回复
热议问题