Introduce
Hello everyone, in the process of learning PHP, many of you must have had difficulty distinguishing $ this and self in PHP and I am no exception. In this article, I hope that the experience gained and the knowledge I have learned can help newcomers to learn PHP language somewhat in distinguishing between $ this and self.
overview
The keywords self and $ this are both used to refer to members of the class within the scope of a class. The members here can be a variable or a function. Self is used to call static components in PHP and $ this is to call non-static components, but if $ experiment can call static property methods, self can call regular attribute methods, we Let’s find out.
1. $ this and self in the object
Have you watched the following example of yourself (the better you do it the better the clown).
- Create a DoAn class and Pho class inherit and override the classType () method of the DoAn class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class DoAn { public function classType() { echo "Đây là DoAn"; } public function echoClass() { self::classType(); } } class Pho extends DoAn { public function classType() { echo "Đây là Pho"; } } $pho = new Pho(); $pho->echoClass(); // kết quả là Đây là DoAn |
In the above code, in my echoClass function, I use self :: classType () so the result will return “This is DoAn” of DoAn class because the echoClass () method is declared in DoAn class, and when I replace self with $ this then:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class DoAn { public function classType() { echo "Đây là DoAn"; } public function echoClass() { $this->classType(); } } class Pho extends DoAn { public function classType() { echo "Đây là Pho"; } } $pho = new Pho(); $pho->echoClass(); // kết quả là Đây là Pho |
The result will now be “This is Pho” because $ this refers to members of the current Pho object, not the DoAn class. Here my brothers have the following conclusions:
* self will reference the class that declared it, $ this will refer to the current object. * self can call non-static methods.
2. $ this and self in the static method
I will go to the next example
1 2 3 4 5 6 7 8 9 10 11 | class DoAn { public static $type; public static function echoClass() { $this->type = 'Do An'; } } $doan = new DoAn(); $doan->echoClass(); // lỗi Fatal error: Uncaught Error: Using $this when not in object context in... |
This error occurs when we use $ this in the static function. Static functions can be called directly from the class without creating objects:
1 2 | DoAn::echoClass(); |
If used in this way, the use of $ this is meaningless, because $ this is used to refer to the current object that we do not work with any object at this time. Now I will replace $ this with self:
1 2 3 4 5 6 7 8 9 10 11 12 | class DoAn { public static $type; public static function echoClass() { echo self::$type = 'Do An'; } } $doan = new DoAn(); $doan->echoClass(); // Do An DoAn::echoClass(); // Do An |
The result of the code runs as normal when the echoClass () method is called in both ways. If I change the static $ type to $ type, the code will get Fatal error: Access to undeclared static property .. because non-static components are not accessible in a static function. We have the following conclusions:
* $ experiments cannot be used in static methods.
3. $ this and self when accessing static methods and static properties
Let’s first see if $ this and self can call static methods or not. I have 2 examples, first use self:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class DoAn { public static function echoClass() { echo 'Do An'; } public function useSelf() { self::echoClass(); } } $doan = new DoAn(); $doan->useSelf(); // kết quả là Do An |
and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class DoAn { public static function echoClass() { echo 'Do An'; } public function useSelf() { $this->echoClass(); } } $doan = new DoAn(); $doan->useSelf(); // kết quả là Do An |
The results of both examples are the same, so:
You can call the static function with $ this
After passing the static methods, I will try with the static property through the example, the first is to use $ this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class DoAn { public static $type = 'Do An'; public function setType($type) { $this->type = $type; } public function echoType() { echo $this->type; } } $doan = new DoAn(); $doan->echoType(); // lỗi Notice: Undefined property: DoAn::$type |
I have declared $ type in class, so why is attribute error not being defined ? In fact, $ type is not $ type in the first line that PHP will automatically create a non-static property named $ type and $ this does not call static properties when replacing $ this with self:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class DoAn { public static $type = 'Do An'; public function setType($type) { self::$type = $type; } public function echoType() { echo self::$type; } } $doan = new DoAn(); $doan->echoType(); // kết quả là Do An |
then the code will run normally. From here we have the following:
$ This cannot be used to access or change the value of a static attribute that is required to use self
Through the examples above, I summarized as follows:
$ this | self |
---|---|
Reference to the components of the current object | Reference to the components of the current class |
Cannot be used in static functions | Can be used inside a static function |
Cannot access or change the value of the static property | Can access and change the value of the static property |
We can also call the static function | We can also call the static function |
Loses the polymorphism |
Thank you for following up on my article, hopefully can help you.