self and $this

What’s the difference between self and $this
Sr. No. $this Self
1 $this refers to the instance of the class. However, there is also something called static state, which is the same for all instances of that class. self:: is the accessor for those attributes and functions.
2 $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.
3 $this is used at the instance-level scope. self is used at the class-level scope.
4 $this is used to reference methods and properties of the current instance of a class. So in other words, use self for static and this for none-static members or methods.
5 $this refers to the current instance of the class. self us used to reference static methods and properties, shared by all instances (and even accessible outside of any instance) of a class.
6 $this refers to the object itself. Self (not $self) refers to the type of class.
7 use $this->member for non-static members use self::$member for static members.
8 $this is used in non-static member functions, and is a reference to the instance of the class on which the member function was called. self refers to the calling object’s class. Use self to refer to the current class.
9 Because this is an object, you use it like: $this->member self is not an object, it’s basically a type that automatically refers to the current class, you use it like: self::member
10   self is for use in static member functions to allow you to access static member variables.
1 $this refers to the instance of the class, that is correct. However, there is also something called static state, which is the same for all instances of that class. self:: is the accessor for those attributes and functions. $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.
2 self is used at the class-level scope whereas $this is used at the instance-level scope. $this is used to reference methods and properties of the current instance of a class.
self us used to reference static methods and properties, shared by all instances (and even accessible outside of any instance) of a class.
3 self (not $self) refers to the type of class, where as $this refers to the current instance of the class. self is for use in static member functions to allow you to access static member variables. $this is used in non-static member functions, and is a reference to the instance of the class on which the member function was called.

Because this is an object, you use it like: $this->member
Because self is not an object, it's basically a type that automatically refers to the current class, you use it like: self::member

self refers to the calling object's class. $this refers to the object itself.

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.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>