What is the difference between using self and $this?

Technology CommunityCategory: PHPWhat is the difference between using self and $this?
VietMX Staff asked 4 years ago

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.

There is another aspect of self:: that is worth mentioning. Annoyingly self:: refers to the scope at the point of definition not at the point of execution. PHP 5.3 has a solution, the static:: resolution operator implements “late static binding” which is a fancy way of saying that it’s bound to the scope of the class called. So $this-> refers to the current object (an instance of a class), whereas static:: refers to a class.