What does $this-> mean in php_PHP Tutorial

WBOY
Release: 2016-07-13 17:45:14
Original
873 people have browsed it

The meaning of $this is to represent the specific object after instantiation!

We usually declare a class first, and then use this class to instantiate objects!

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

For example:

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

class User
{
Public $_name;
}

Now, I'll add a method to the User class. Just use the getName() method to output the value of the $name attribute! Copy PHP content to clipboard
PHP code:
class User
{
Public $name;

Function getName()
          {
echo $this->name;
}
}

//How to use it?

$user1 = new User();

$user1->name = 'Zhang San';

$user1->getName(); // Zhang San will be output here!

$user2 = new User();

$user2->name = '李思';

$user2->getName(); //John Doe will be output here!

How to understand it?

I created two User objects above. They are $user1 and $user2 respectively.

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

That’s probably what it means!

In fact, you don’t want to get into trouble. You just need to know that it is a code name used to represent the properties and methods inside the class! The more I think about it, the more confused I become!
Found it somewhere else! ! Stay! !

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478685.htmlTechArticle$this means the specific object after instantiation! We usually declare a class first, and then use this class to instantiate objects! However, when we declare this class, we want to...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template