A detailed explanation of $this usage in PHP

Source: Internet
Author: User
This is a pointer to the current object

<?phpclass human{public $name = "John Doe";p ublic function Eat () {  echo $this->name, ' eating ';} Public Function test () {  echo $name, ' at Dinner ';}} $li =new Human (); Echo $li->name;//John Doe $li->eat ();//John Doe at dinner. /* Compared to java,c++ the method body wants to access the caller's properties, it must be used $this if not added, then understood as a local variable inside the method. */$li->test ();//notice:undefined Variable:name in C: wamp\www\php\this.php on line 14/****** from the perspective of life to understand $this Nu wa created a person, made a "remorse" method {    catch [their] hair [    their] face} The world so many people, regret, Who's got the hair? Who's the face? Zhang San, John Doe? Harry? Can not explain that a reasonable situation can only be understood as "self" ******/?>


This article details PHP $this usage, more relevant content please focus on the PHP Chinese web.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.