什么时候在PHP中使用$this关键字?

什么时候在PHP中使用$this关键字?,php,oop,Php,Oop,什么时候在PHP中使用$this关键字?据我所知,$此指在不知道对象名称的情况下创建的对象 关键字$this也只能在方法中使用 举例说明何时可以使用$this它用于面向对象编程(OOP): 类可以包含自己的常量、变量(称为“属性”)和函数(称为“方法”) $this伪变量的一些示例: <?php class A { function foo() { if (isset($this)) { echo '$this is define

什么时候在PHP中使用
$this
关键字?据我所知,
$此
指在不知道对象名称的情况下创建的对象

关键字
$this
也只能在方法中使用


举例说明何时可以使用
$this

它用于面向对象编程(OOP):


类可以包含自己的常量、变量(称为“属性”)和函数(称为“方法”)


$this伪变量的一些示例:

<?php
class A
{
    function foo()
    {
        if (isset($this)) {
            echo '$this is defined (';
            echo get_class($this);
            echo ")\n";
        } else {
            echo "\$this is not defined.\n";
        }
    }
}

class B
{
    function bar()
    {
        // Note: the next line will issue a warning if E_STRICT is enabled.
        A::foo();
    }
}

$a = new A();
$a->foo();

// Note: the next line will issue a warning if E_STRICT is enabled.
A::foo();
$b = new B();
$b->bar();

// Note: the next line will issue a warning if E_STRICT is enabled.
B::bar();
?>

上述示例将输出:

  • $this定义为(A)
  • $this未定义
  • $这是定义的(B)
  • $this未定义

如果您正在用PHP进行面向对象编程,则只能使用$this。这意味着如果您正在创建类。以下是一个例子:

class Item {
  protected $name, $price, $qty, $total;

  public function __construct($iName, $iPrice, $iQty) {
    $this->name = $iName;
    $this->price = $iPrice;
    $this->qty = $iQty;
    $this->calculate();
  }

}

有一次我知道我最终使用了
这个
在其他语言中的等价物是实现一个“流畅”的界面;每个类方法返回
void
,而不是返回
this
,这样方法调用可以很容易地链接在一起

public function DoThis(){
    //Do stuff here...
    return $this;
}
public function DoThat(){
   //do other stuff here...
   return $this;
}
上述情况可以这样称呼:

myObject->DoThis()->DoThat();

这可能对某些事情有用。

不,我认为你的想法是错误的
$this
在引用同一类的对象时使用。。像这样

假设我们有一个变量值$var,在中,该对象的实例应设置为5


$this->var=5

最常见的用例是在面向对象编程中定义类或在类中工作。例如:

class Horse {
    var $running = false;

    function run() {
        $this->running = true;
    }
}

如您所见,在
run
函数中,我们可以使用
$this
变量来引用我们“所在”的Horse类的实例。所以要记住的另一件事是,如果您创建了两个Horse类,那么每个类中的
$this
变量将引用Horse类的特定实例,而不是同时引用它们。

用于处理局部变量

您还可以从中阅读更多有关它的信息


use$this用于引用属于当前对象的方法或实例变量

$this->name=$name 或 $this->callsomethod()

这将使用当前对象子类中实现的变量或方法

如果您想专门调用父类的实现,您可以执行以下操作


parent::callsomethod()

$用于引用对象的当前实例。 因此,您可以执行以下操作:

class MyClass {
    private $name;

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

    //vs
    public function setName($pName) {
        $name = $pName;
    }
}
另一个很酷的用途是,您可以链接方法:

class MyClass2 {
    private $firstName;
    private $lastName;

    public function setFirstName($name) {
        $this->firstName = $name;
        return $this;
    }

    public function setLastName($name) {
        $this->lastName = $name;
        return $this;
    }

    public function sayHello() {
        print "Hello {$this->firstName} {$this->lastName}";
    }
}

//And now you can do:
$newInstance = new MyClass2;
$newInstance->setFirstName("John")->setLastName("Doe")->sayHello();
$this在创建对象的新实例时使用

例如,想象一下:

class Test {
    private $_hello = "hello";

    public function getHello () {
        echo $this->_hello; // note that I removed the $ from _hello !
    }

    public function setHello ($hello) {
        $this->_hello = $hello;
    }
}
为了访问getHello方法,我必须创建一个新的类测试实例,如下所示:

$obj = new Test ();
// Then, I can access to the getHello method :
echo $obj->getHello ();
// will output "hello"

$obj->setHello("lala");
echo $obj->getHello ();
// will output "lala"    
事实上,$this在实例化时在类内部使用。它被称为范围

在类内部使用$this(例如,用于访问*$\u hello*),但在类外部,$this并不引用类内部的元素(比如*$\u hello*),而是$obj

现在,$obj和$this之间的主要区别在于,$obj从外部访问您的类,因此会出现一些限制:如果您在类中定义了私有的受保护的,例如我的变量*$\u hello*,$obj无法访问它(它是私有的!)但是$this可以,因为$this会留在课堂内。


<?php
  class identity {
    public $name;
    public $age;
    public function display() {
      return $this->name . 'is'. $this->age . 'years old';
    }
  }
?>

每当您想要使用函数外部但在同一类内部的变量时,您都可以使用$this$这是指要访问的属性或函数所在的当前php类。它是php的核心概念。

->基础知识。特别是,请注意这里的示例2:
$this
在php中不是关键字(而是伪变量),因此无法回答您的问题。谢谢!我理解,但是你会在方法之外使用$this吗?正确,你不会,因为$this只在类中有意义。除此之外,您可以使用对象,比如$a=new Item(),您可以执行$a->methodName(),您的意思是可以像
myObject->DoThis()->DoThat()
;-)那样调用它@多姆-呵呵。。。很抱歉我很少使用PHP,几乎从不使用OOP/PHP。我会解决的。。。谢谢你提醒我那个愚蠢的错误!:)没问题。。。我只是好奇,不再自信该怎么做:P//EDIT$myObject->DoThis()->DoThat()当然了,哈哈!
class Test {
    private $_hello = "hello";

    public function getHello () {
        echo $this->_hello; // note that I removed the $ from _hello !
    }

    public function setHello ($hello) {
        $this->_hello = $hello;
    }
}
$obj = new Test ();
// Then, I can access to the getHello method :
echo $obj->getHello ();
// will output "hello"

$obj->setHello("lala");
echo $obj->getHello ();
// will output "lala"    
<?php
  class identity {
    public $name;
    public $age;
    public function display() {
      return $this->name . 'is'. $this->age . 'years old';
    }
  }
?>