Home  >  Article  >  Backend Development  >  What does $this-) mean in php? _PHP Tutorial

What does $this-) mean in php? _PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:35:46918browse

What does $this mean, 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:

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

classname

{

public $_name;

}

Now we 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='Zhang San';

$user1->getName();

$user2=new User();

$user2->name='李思';

$user2-getName();

How to understand?

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

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!

Actually, you shouldn’t go too far. 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!


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/742412.htmlTechArticleWhat does $this mean, 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...
Statement:
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