" 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

What is the $this-> method in php

WBOY
WBOYOriginal
2022-03-07 12:00:503730browse

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.

What is the $this-> method in php

The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.

What is the $this-> method in php

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 = &#39;张三&#39;;
$user1->getName();        //这里就会输出张三!
$user2 = new User();
$user2->name = &#39;李四&#39;;  
$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!

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