Home >Backend Development >PHP Tutorial >$this usage in PHP_PHP tutorial

$this usage in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:11:241219browse

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"
******/
?>

$this usage in PHP_PHP tutorial

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477337.htmlTechArticlethis is a pointer to the current object [php] view plaincopyprint? ?php class human{ public $name=李思; public function eat(){ echo $this-name, eating; } public function test(){ echo...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn