The meaning of $this is to represent the specific object after instantiation!
We usually declare a class first, and then use this class to instantiate objects!
However, when we declare this class, we want to use the properties or methods of this class within the class itself. How should it be expressed?
For example:
I declare a User class! It only contains one attribute $name;
class User
{
Public $_name;
}
Now, I'll add a method to the User class. Just use the getName() method to output the value of the $name attribute! Copy PHP content to clipboard
PHP code:
class User
{
Public $name;
Function getName()
{
echo $this->name;
}
}
//How to use it?
$user1 = new User();
$user1->name = 'Zhang San';
$user1->getName(); // Zhang San will be output here!
$user2 = new User();
$user2->name = '李思';
$user2->getName(); //John Doe will be output here!
How to understand it?
I created two User objects above. They are $user1 and $user2 respectively.
When I call $user1->getName(). The code in the User class above echo $this->name ; is equivalent to echo $user1->name;
That’s probably what it means!
In fact, you don’t want to get into trouble. You just need to know that it is a code name used to represent the properties and methods inside the class! The more I think about it, the more confused I become!
Found it somewhere else! ! Stay! !