When to use self over $this?

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

    From this blog post:

    • self refers to the current class
    • self can be used to call static functions and reference static member variables
    • self can be used inside static functions
    • self can also turn off polymorphic behavior by bypassing the vtable
    • $this refers to the current object
    • $this can be used to call static functions
    • $this should not be used to call static member variables. Use self instead.
    • $this can not be used inside static functions

提交回复
热议问题