" means using the attributes or methods of this class within the class itself; "$this" means the specific object after instantiation, and "->" is the plug-in dereference operator, which is called by A method that references a subroutine to which arguments are passed."/> " means using the attributes or methods of this class within the class itself; "$this" means the specific object after instantiation, and "->" is the plug-in dereference operator, which is called by A method that references a subroutine to which arguments are passed.">
Home >Backend Development >PHP Problem >What is the $this-> method in php
In PHP, "$this->" means using the attributes or methods of this class within the class itself; "$this" means the specific object after instantiation, and "->" is plug-in The dereference operator is a method of calling a subroutine whose parameters are passed by reference.
The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.
In php we usually declare a class first, and then use this class to instantiate objects! The meaning of $this is to represent the specific object after instantiation! $this-> means using the attributes or methods of this class within the class itself. The ‘->’ symbol is the “infix dereference operator”. In other words, it is a method that calls a subroutine whose parameters are passed by reference (among other things, of course). As we mentioned above, when calling PHP functions, most parameters are passed by reference.
For example, we declare a User class! It only contains one attribute $name;
<?php class User { public $_name; } ?>
Now, we add a method to the User class. Just use the getName() method to output the value of the $name attribute!
<?php class User { public $name; function getName() { echo $this->name; } } //如何使用呢? $user1 = new User(); $user1->name = '张三'; $user1->getName(); //这里就会输出张三! $user2 = new User(); $user2->name = '李四'; $user2->getName(); //这里会输出李四! ?>
Two User objects are created above. They are $user1 and $user2 respectively.
When calling $user1->getName(). The code in the User class above echo $this->name ; is equivalent to echo $user1->name;
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the $this-> method in php. For more information, please follow other related articles on the PHP Chinese website!