name,'Eating'; } public function test(){ echo $name, 'Eating'; } }$li=new human();echo $li->n"/> name,'Eating'; } public function test(){ echo $name, 'Eating'; } }$li=new human();echo $li->n">
Home > Article > Backend Development > Detailed explanation of $this usage in PHP
this is a pointer to the current object
<?php class human{ public $name="李四"; public function eat(){ echo $this->name,'在吃饭'; } public function test(){ echo $name,'在吃饭'; } } $li=new human(); echo $li->name;//李四 $li->eat();//李四在吃饭。 /* 和java,c++相比 方法体内想访问调用者的属性,必须用$this 如果不加,则理解为方法内部的一个局部变量. */ $li->test();//Notice: Undefined variable: name in C:\wamp\www\php\this.php on line 14 /****** 从生活中的角度来理解$this 女娲造人时, 造了一个"悔恨"的方法 { 抓[自己]头发 抽 [自己] 脸 } 世界上的人那么多, 悔恨时,抓谁的头发? 抽谁的脸? 张三,李四? 王五? 都不能说明合理的情况 只能理解为"自己" ******/ ?>
This article explains in detail the usage of $this in PHP. For more related content, please pay attention to php Chinese website.
Related recommendations:
The relationship between Java and PHP
##Summary of practical experience with PHP
The above is the detailed content of Detailed explanation of $this usage in PHP. For more information, please follow other related articles on the PHP Chinese website!