What do you mean by $this-in PHP? _php Tutorials

Source: Internet
Author: User
The meaning of $this means what to instantiate after the specific object!

We usually declare a class first, then use this class to instantiate an Object!

However, when we declare this class, we want to use the properties or methods of this class within the class itself. What should be said?

For example:

We declare a user class! It contains only one attribute $name;

ClassName

{

Public $_name;

}

Now let's add a method to the user class. Use the GetName () method to output the value of the $name Property! Copy PHP content to clipboard

PHP Code:

Class User

{

Public $name;

function GetName ()

{

Echo $this->name;

}

}


How to use it?

$user 1=new User ();

$user 1->name= ' Zhang San ';

$user 1->getname ();

$user 2=new User ();

$user 2->name= ' John Doe ';

$user 2-getname ();

How do you understand it?

I created two user objects above. $user1 and $user2, respectively.

When I call $user1->getname (). The code in the user class above Echo $this->name, which is equivalent to echo $user 1->name;

That's probably what it means!

In fact, you also do not go to the dead. All you need to know is that it's a code to represent the properties and methods inside the class! The more you think, the more confused!


http://www.bkjia.com/PHPjc/742412.html www.bkjia.com true http://www.bkjia.com/PHPjc/742412.html techarticle the meaning of $this means what to instantiate after the specific object! We usually declare a class first, then use this class to instantiate an Object! But when we declare this class ...

  • 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.