119

我一直在 PHP 中看到这个变量$this,但我不知道它的用途。我从来没有亲自使用过它。

有人能告诉我这个变量$this在 PHP 中是如何工作的吗?

4

11 回答 11

144

它是对当前对象的引用,最常用于面向对象的代码中。

例子:

<?php
class Person {
    public $name;

    function __construct( $name ) {
        $this->name = $name;
    }
};

$jack = new Person('Jack');
echo $jack->name;

这会将“Jack”字符串存储为所创建对象的属性。

于 2009-10-06T03:46:30.463 回答
44

了解$thisPHP 中变量的最佳方法是在各种上下文中针对解释器进行尝试:

print isset($this);              //true,   $this exists
print gettype($this);            //Object, $this is an object 
print is_array($this);           //false,  $this isn't an array
print get_object_vars($this);    //true,   $this's variables are an array
print is_object($this);          //true,   $this is still an object
print get_class($this);          //YourProject\YourFile\YourClass
print get_parent_class($this);   //YourBundle\YourStuff\YourParentClass
print gettype($this->container); //object
print_r($this);                  //delicious data dump of $this
print $this->yourvariable        //access $this variable with ->

所以$this伪变量具有当前对象的方法和属性。这样的东西很有用,因为它允许您访问类中的所有成员变量和成员方法。例如:

Class Dog{
    public $my_member_variable;                             //member variable

    function normal_method_inside_Dog() {                   //member method

        //Assign data to member variable from inside the member method
        $this->my_member_variable = "whatever";

        //Get data from member variable from inside the member method.
        print $this->my_member_variable;
    }
}

$this是对解释器为您创建的 PHP 的引用Object,其中包含变量数组。

如果$this在普通类的普通方法内部调用,$this则返回该方法所属的 Object(类)。

$this如果上下文没有父对象,则 可能未定义。

php.net 有一个大页面讨论 PHP 面向对象编程以及如何$this根据上下文进行行为。 https://www.php.net/manual/en/language.oop5.basic.php

于 2013-12-11T18:23:58.553 回答
17

我知道它的老问题,无论如何另一个关于$this的确切解释。$this主要用于引用类的属性。

例子:

Class A
{
   public $myname;    //this is a member variable of this class

function callme() {
    $myname = 'function variable';
    $this->myname = 'Member variable';
    echo $myname;                  //prints function variable
    echo $this->myname;              //prints member variable
   }
}

输出:

function variable

member variable
于 2015-02-24T06:41:21.583 回答
9

它是从自身内部引用类实例的方式,与许多其他面向对象的语言相同。

来自PHP 文档

当从对象上下文中调用方法时,伪变量 $this 可用。$this 是对调用对象的引用(通常是方法所属的对象,但也可能是另一个对象,如果该方法是从辅助对象的上下文中静态调用的)。

于 2009-10-06T03:47:23.100 回答
7

让我们看看如果我们不使用 $this 并尝试使用以下代码片段使实例变量和构造函数参数具有相同名称会发生​​什么

<?php

class Student {
    public $name;

    function __construct( $name ) {
        $name = $name;
    }
};

$tom = new Student('Tom');
echo $tom->name;

?>

它只呼应

<?php

class Student {
    public $name;

    function __construct( $name ) {
        $this->name = $name; // Using 'this' to access the student's name
    }
};

$tom = new Student('Tom');
echo $tom->name;

?>

这与“汤姆”相呼应

于 2016-06-19T14:59:00.467 回答
4

当你创建一个类时,你有(在许多情况下)实例变量和方法(又名函数)。$this 访问这些实例变量,以便您的函数可以获取这些变量并执行它们需要对它们执行的任何操作。

meder 示例的另一个版本:

class Person {

    protected $name;  //can't be accessed from outside the class

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}
// this line creates an instance of the class Person setting "Jack" as $name.  
// __construct() gets executed when you declare it within the class.
$jack = new Person("Jack"); 

echo $jack->getName();

Output:

Jack
于 2009-10-06T14:47:10.203 回答
4

这是很长的详细解释。我希望这对初学者有帮助。我会让它变得非常简单。

首先,让我们创建一个类

<?php 

class Class1
{
    
}

?>如果您只使用 php 代码,则可以省略 php 结束标记。

现在让我们在里面添加属性和方法Class1

<?php 

class Class1
{
    public $property1 = "I am property 1";
    public $property2 = "I am property 2";

    public function Method1()
    {
        return "I am Method 1";
    }
}

该属性只是一个简单的变量,但我们将其命名为属性,因为它位于一个类中。

该方法只是一个简单的函数,但我们说方法是因为它也在一个类中。

public关键字意味着可以在脚本中的任何位置访问方法或属性。

现在,我们如何使用里面的属性和方法Class1呢?

答案是创建实例或对象,将对象视为类的副本。

<?php 

class Class1
{
    public $property1 = "I am property 1";
    public $property2 = "I am property 2";

    public function Method1()
    {
        return "I am Method 1";
    }
}

$object1 = new Class1;
var_dump($object1);

我们创建了一个对象,即,它是包含其所有内容$object1的副本。Class1我们转储了$object1using的所有内容var_dump()

这会给你

object(Class1)#1 (2) { ["property1"]=> string(15) "I am property 1" ["property2"]=> string(15) "I am property 2" }

所以所有的内容Class1都在$object1,除了Method1,我不知道为什么在转储对象时方法不显示。

现在,如果我们只想访问怎么办$property1。它很简单,我们做var_dump($object1->property1);,我们只是添加->property1,我们指向它。

我们也可以访问Method1(),我们做var_dump($object1->Method1());

现在假设我想$property1从内部访问Method1(),我会这样做

<?php 

class Class1
{
    public $property1 = "I am property 1";
    public $property2 = "I am property 2";

    public function Method1()
    {   
        $object2 = new Class1;
        return $object2->property1;
    }
}

$object1 = new Class1;
var_dump($object1->Method1()); 

我们创建$object2 = new Class1;了一个新副本,Class1或者我们可以说一个实例。然后我们指向property1from$object2

return $object2->property1;

string(15) "I am property 1"这将在浏览器中打印。

现在而不是在里面做这个Method1()

$object2 = new Class1;
return $object2->property1;

我们这样做

return $this->property1;

$this对象在类内部用于引用类本身。

它是创建新对象然后像这样返回它的替代方法

$object2 = new Class1;
return $object2->property1;

另一个例子

<?php 

class Class1
{
    public $property1 = 119;
    public $property2 = 666;
    public $result;

    public function Method1()
    {   
        $this->result = $this->property1 + $this->property2;
        return $this->result;
    }
}

$object1 = new Class1;
var_dump($object1->Method1());

我们创建了 2 个包含整数的属性,然后我们将它们相加并将结果放入$this->result.

不要忘记

$this->property1= $property1=119

它们具有相同的价值..等

我希望这能解释这个想法。

这一系列视频将对您在 OOP 方面有很大帮助

https://www.youtube.com/playlist?list=PLe30vg_FG4OSEHH6bRF8FrA7wmoAMUZLv

于 2020-06-22T06:51:49.137 回答
3

$this对调用对象的引用(通常是方法所属的对象,但也可能是另一个对象,如果该方法是从辅助对象的上下文中静态调用的)。

于 2015-10-29T14:18:18.870 回答
2

$this 是一个特殊变量,它指的是同一个对象,即。本身。

它实际上是指当前类的实例

这是一个可以清除上述语句的示例

<?php
 class Books {
  /* Member variables */
  var $price;
  var $title;

  /* Member functions */
  function setPrice($par){
     $this->price = $par;
  }

  function getPrice(){
     echo $this->price ."<br/>";
  }

  function setTitle($par){
     $this->title = $par;
  }

  function getTitle(){
     echo $this->title ." <br/>";
  }
}
?> 
于 2016-03-26T07:20:44.723 回答
1

正如meder所说,它指的是当前类的实例。

请参阅PHP 文档。在第一个示例中进行了解释。

于 2009-10-06T03:47:47.093 回答
0

这个关键字一般用在类内部,一般用在成员函数中,用于访问当前对象的类(变量或函数)的非静态成员。

  1. 这个关键字前面应该有一个 $ 符号。
  2. 对于此运算符,我们使用 -> 符号。
  3. 而 $this 将引用特定实例的成员变量和函数。

我们举个例子来了解一下$this的用法。

<?php
class Hero {
    // first name of hero
    private $name;
    
    // public function to set value for name (setter method)
    public function setName($name) {
        $this->name = $name;
    }
    
    // public function to get value of name (getter method)
    public function getName() {
        return $this->name;
    }
}

// creating class object
$stark = new Hero();

// calling the public function to set fname
$stark->setName("IRON MAN");

// getting the value of the name variable
echo "I Am " . $stark->getName();
?>

输出:我是钢铁侠

注意:静态变量充当全局变量,并在类的所有对象之间共享。非静态变量特定于创建它们的实例对象。

于 2020-12-13T17:02:07.023 回答