What does $this mean, 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:
We declare a User class! It only contains one attribute $name;
classname
{
public $_name;
}
Now we add a method to the User class. Use the getName() method to output the value of the $name attribute! Copy the php content to the 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();
$user2=new User();
$user2->name='李思';
$user2-getName();
How to understand?
I created two User objects above. They are $user1 and $user2.
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!
Actually, you shouldn’t go too far. 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!