在PHP中,什么时候需要使用$this
关键字?据我所知,$this
指的是在不知道对象名称的情况下创建的对象。
此外,$this
关键字只能在方法内部使用?
一个示例可以很好地展示何时可以使用$this
。
一个类可以包含它自己的常量、变量(被称为“属性”)和函数(被称为“方法”)。
<?php
class SimpleClass
{
// property declaration
public $var = 'a default value';
// method declaration
public function displayVar() {
echo $this->var;
}
}
?>
$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();
?>
class Horse {
var $running = false;
function run() {
$this->running = true;
}
}
正如你所看到的,在run
函数中,我们可以使用$this
变量来引用当前实例化的Horse类。因此,需要记住的另一件事是,如果您创建了2个Horse类,每个类内部的$this
变量将分别引用该特定实例化的Horse类,而不是这两个类。
class Item {
protected $name, $price, $qty, $total;
public function __construct($iName, $iPrice, $iQty) {
$this->name = $iName;
$this->price = $iPrice;
$this->qty = $iQty;
$this->calculate();
}
}
$this用于引用当前对象的实例。因此,您可以执行以下操作:
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();
它在面向对象编程(OOP)中使用:
<?php
class Example
{
public function foo()
{
//code
}
public function bar()
{
$this->foo();
}
}
this
等效物来实现“流畅”接口;每个类方法本来应该返回void
,而是返回this
,以便可以轻松地链接方法调用。public function DoThis(){
//Do stuff here...
return $this;
}
public function DoThat(){
//do other stuff here...
return $this;
}
可以这样调用上述内容:
myObject->DoThis()->DoThat();
这对某些事情可能会有用。
myObject->DoThis()->DoThat()
;-)? - thedom$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;
}
}
$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(例如访问* $_hello *),但在类外部,$this不引用类内部的元素(如* $_hello *),而是$obj。
现在,$obj和$this之间的主要区别是,由于$obj从外部访问您的类,因此会有一些限制:如果您在类中定义了某些私有或受保护的内容,例如我的变量* $_hello *,$obj 无法访问它(它是私有的!)但是$this可以,因为$this在类内部。
不,我认为你的想法是错误的... $this
是用来引用同一类的对象的.. 就像这样
假设我们有一个变量值 $var,并且在那个对象实例中应该设置为 5
$this->var=5;
class
关键字:class MyClass { public $myAttribute = 'default value'; public function myMethod() { echo 'Hello World!'; } }
在这个例子中,我们定义了一个名为MyClass
的类。它有一个名为$myAttribute
的公共属性和一个名为myMethod()
的公共方法。要实例化该类并访问其属性和方法,可以使用以下代码:$obj = new MyClass(); echo $obj->myAttribute; // 输出 "default value" $obj->myMethod(); // 输出 "Hello World!"
- Your Common Sense$this
在 PHP 中不是一个关键字(而是一个伪变量),因此无法回答您的问题。 - hakre