What does $ this-& gt; in php mean? _ PHP Tutorial

Source: Internet
Author: User
In php, what does $ this-& amp; gt; mean. $ This indicates the specific object after instantiation! We generally declare a class first, and then use this class to instantiate the object! However, when we declare this class, we think $ this indicates the specific object after instantiation!

We generally declare a class first, and then use this class to instantiate the object!

However, when we declare this class, we want to use the attributes or methods of this class in the class itself. What should I do?

For example:

I declare a User class! It only contains one attribute $ name;

Class User
{
Public $ _ name;
}

Now, I 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 = 'zhangsan ';

$ User1-> getName (); // three items are output here!

$ User2 = new User ();

$ User2-> name = 'Li si ';

$ User2-> getName (); // Li Si will be output here!

How can this problem be solved?

I created two User objects above. $ User1 and $ user2 respectively.

When I call $ user1-> getName. The above User class code echo $ this-> name; is equivalent to echo $ user1-> name;

This is probably the meaning!

In fact, you do not want to go to the tip. You only need to know that it is a code used to indicate the internal attributes and methods of the class! The more you think about it, the more confused you are!
Found elsewhere !! Left !!

The definition of objects is the specific object after instantiation! We generally declare a class first, and then use this class to instantiate the object! However, when we declare this class, we want...

Related Article

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.