What is the difference between self and $this in PHP5?

Can someone please explain the difference between using self and $this and what are the scenarios that each of these used?


  • This page says.

    Use $this to refer to the current object. Use self to refer to the current class. In other words, use $this->member for non-static members, use self::$member for static members.

  • $this refers to the current object, self refers to the current class. 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.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!