Distinguish $ this and self in PHP

Tram Ho

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.

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:

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

This error occurs when we use $ this in the static function. Static functions can be called directly from the class without creating objects:

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:

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:

and

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:

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:

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:

$ thisself
Reference to the components of the current objectReference to the components of the current class
Cannot be used in static functionsCan be used inside a static function
Cannot access or change the value of the static propertyCan access and change the value of the static property
We can also call the static functionWe can also call the static function
Loses the polymorphism

Thank you for following up on my article, hopefully can help you.

Share the news now

Source : Viblo