Home >Backend Development >PHP Tutorial >$this usage in PHP_PHP tutorial
this is a pointer to the current object [php] view plaincopyprint?
class human{
Public $name="李思";
Public function eat(){
echo $this->name,'Eating';
}
Public function test(){
echo $name,'Eating';
}
}
$li=new human();
echo $li->name;//李思
$li->eat();//Li Si is eating.
/*
Compared with java and c++
If you want to access the caller's attributes within the method body, you must use $this
If not added, it will be understood as a local variable inside the method.
*/
$li->test();//Notice: Undefined variable: name in C:wampwwwphpthis.php on line 14
/******
Understand $this
from a life perspective
When Nuwa created humans, she created a method of "regret"
{
Pulling [one’s] hair
Slap [yourself] in the face
}
There are so many people in the world,
Whose hair do you grab when you feel regretful?
Whose face should I slap?
Zhang San, Li Si? Wang Wu? None of them can explain the reasonable situation
It can only be understood as "self"
******/
?>
class human{
public $name="李思";
public function eat(){
echo $this->name,'Eating';
}
public function test(){
echo $name,'Eating';
}
}
$li=new human();
echo $li->name;//李思
$li->eat();//Li Si is eating.
/*
Compared with java and c++
If you want to access the caller's attributes within the method body, you must use $this
If not added, it will be understood as a local variable inside the method.
*/
$li->test();//Notice: Undefined variable: name in C:wampwwwphpthis.php on line 14
/******
Understand $this
from a life perspective
When Nuwa created humans, she created a method of "regret"
{
Pulling [one’s] hair
Slap [yourself] in the face
}
There are so many people in the world,
Whose hair do you grab when you feel regretful?
Whose face should I slap?
Zhang San, Li Si? Wang Wu? None of them can explain a reasonable situation
It can only be understood as "self"
******/
?>