简体   繁体   English

$ this是什么意思

[英]What is the meaning of $this

I've seen some scripts contain $this in a PHP script that has OOP, I never knew the meaning of it... like 我已经看到一些脚本在包含OOP的PHP脚本中包含$this ,我从来不知道它的含义......就像

$this->refresh();

Perhaps explain to me what $this refers to be...? 也许向我解释一下$this是什么意思......?

But I do know that you cannot use it as a dynamic variable like $this_is_a_variable but why can't use it as a dynamic variable? 但我知道你不能将它用作动态变量,如$this_is_a_variable但为什么不能将它用作动态变量呢?

$this is a reference to the current object. $this是对当前对象的引用。

It can be used in class methods only. 它只能在类方法中使用。

From the manual: 手册:

The pseudo-variable $this is available when a method is called from within an object context. 当从对象上下文中调用方法时,伪变量$ this可用。 $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object). $ this是对调用对象的引用(通常是方法所属的对象,但如果从辅助对象的上下文中静态调用该方法,则可能是另一个对象)。

a simple real world example: 一个简单的现实世界示例:

class Classname
{
  private $message = "The big brown fox... jumped....";

  function setMessage($givenMessage) {
    $this->message = $givenMessage;
  }

  function getMessage() {
    return $this->message;  // Will output whatever value 
                            // the object's message variable was set to
  }
}

$my_object = new Classname();  // this is a valid object
echo $my_object->getMessage();  // Will output "The big brown fox... jumped...."

$my_object->setMessage("Hello World!");
echo $my_object->getMessage();  // Will output "Hello world"

$this is not available when you call a method in a static context: 静态上下文中调用方法时, $this 不可用:

Classname::showMessage(); // Will throw an error: 
                          // `$this` used while not in object context

If you are doing OOP then you use classes. 如果您正在进行OOP,那么您可以使用类。 You can have: 你可以有:

class CFoo
  {
  private $var;
  public function setFoo($fooVal)
    {
    $this->var = $fooVal;
    }
  }

$this refers to the current object of that class. $ this指的是该类的当前对象。

When creating classes within PHP, at times you may need to reference the class* itself. 在PHP中创建类时,有时您可能需要引用类*本身。 The $this variable is reserved for this purpose. $this变量保留用于此目的。

**This should be correct as 'referring to the object created' not the class. **这应该是正确的'引用创建的对象'而不是类。 This is semantically more correct.* 这在语义上更正确。*

For example: 例如:

class Car
{
    private $make;

    public function setMake($make)
    {
         $this->make = $make;
    }

    public function setModel($model)
    {
         $this->model = $model;
    }

    public function whatCar()
    {
        return "This car is a " . $this->make . " " . $this->model;
    }
}

And to use it would look something like: 并使用它看起来像:

$car = new Car();

$car->setMake('Ford');
$car->setModel('Escort');

echo $car->whatCar();
//This car is a Ford Escort

$this refers to the current object of the class. $ this指的是该类的当前对象。 $this is used in methods which are members of a particular class. $ this用于特定类成员的方法中。 Hence, inside those methods, the method already has the information about the particular "instance" of that class. 因此,在这些方法中,该方法已经具有关于该类的特定“实例”的信息。 So $this can be directly used to refer the current object, rather than retrieving and assigning an object to a different variable. 所以$ this可以直接用于引用当前对象,而不是检索对象并将其分配给不同的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM