In plain English, how is $this
used in PHP?
简单地说,在PHP中如何使用$this ?
It's such a simple concept to me in JavaScript, but for some reason in PHP I can't get my head around this variable and its function. What, at any given point, is it referring to exactly? I have only tertiary experience with OOP, and I suspect that's why it's hard for me to understand its usage, but I'm trying to get better, and a lot of the code I examine uses this variable.
在JavaScript中,这是一个非常简单的概念,但是由于PHP的原因,我无法理解这个变量及其函数。在任何给定的点上,它到底指的是什么?我在OOP方面只有三次经验,我怀疑这就是为什么我很难理解它的用法,但我正在努力做得更好,我检查的很多代码都使用了这个变量。
2 个解决方案
#1
15
In Very Simple English:
Once inside an object's function, you have complete access to its variables, but to set them you need to be more specific than just using the variable name you want to work with. To properly specify you want to work with a local variable, you need to use the special $this
variable, which PHP always sets to point to the object you are currently working with.
一旦进入一个对象的函数,您就可以完全访问它的变量,但是要设置它们,您需要比仅仅使用您想要使用的变量名更加具体。要正确指定要使用局部变量,需要使用特殊的$this变量,PHP总是将它设置为指向当前正在处理的对象。
For example:
例如:
function bark()
{
print "{$this->Name} says Woof!\n";
}
Whenever you are inside a function of an object, PHP automatically sets the $this
variable contains that object. You do not need to do anything to have access to it.
无论何时在对象的函数中,PHP都会自动设置$this变量包含该对象。你不需要做任何事情来访问它。
In Normal English:
$this
is a pseudo-variable which is available when a method is called from within an object context. It 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是一个伪变量,当从对象上下文中调用方法时可用。它是对调用对象的引用(通常是方法所属的对象,但如果从辅助对象的上下文中静态地调用方法,则可能是另一个对象)
An example:
一个例子:
<?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();
?>
Output:
输出:
$this is defined (A)
$this is not defined.
$this is defined (B)
$this is not defined.
#2
2
To elaborate shamittomar:
阐述shamittomar:
$this is a handle to be able to refer to the current object where the call is done in. (so it basically points to itself. Say we have multiple objects of the same class, and we wanted to set it up with (different) data before we do the ultimate move: echo it. It would be hard to point to itself when you don't know the object name.
$这是一个句柄,可以引用调用所在的当前对象。(所以它基本上指向自己。假设我们有同一个类的多个对象,我们想在执行最终操作之前用(不同的)数据来设置它:echo它。如果不知道对象名,就很难指出它自己。
class SaySomething{
private $the_line;// the variable exists only in this class!
public function __construct($myline){
$this->the_line = $myline;
// see how it points to itself?
// would there be a variable in the global scope then it would be not possible to "setup"
// the object without it getting overwritten in the next setup.
}
//The function to echo the stuf. Can be callid by the "outside world"
public function say_it(){
echo $this->the_line;
$this->add_exclamation();//call the function add_exclamation in this class/object.
}
//This function can not be called by the outside world because it's private.
// The only way to call it is from inside this class. To point to this class and call the function:
// $this->add_exclamation();
private function add_exclamation(){
echo "!";
}
}
$obja = new SaySomething('my');
$objb = new SaySomething('sample');
$objc = new SaySomething('super');
$objd = new SaySomething('text');
//Mind: uptill nothing has been said, only the private variable $the_line has been set in the constructor.
$obja->say_it();
$objc->say_it();
$objb->say_it();
$objd->say_it();
To understand what a class and what an object is (they tend to be mixed up a lot...) watch this slideshow: http://www.slideshare.net/sebastian_bergmann/understanding-the-php-object-model
要了解什么是类,什么是对象(它们经常被混淆…),请查看这个幻灯片:http://www.slideshare.net/sebastian_bergmann/understanding- php-object-model
#1
15
In Very Simple English:
Once inside an object's function, you have complete access to its variables, but to set them you need to be more specific than just using the variable name you want to work with. To properly specify you want to work with a local variable, you need to use the special $this
variable, which PHP always sets to point to the object you are currently working with.
一旦进入一个对象的函数,您就可以完全访问它的变量,但是要设置它们,您需要比仅仅使用您想要使用的变量名更加具体。要正确指定要使用局部变量,需要使用特殊的$this变量,PHP总是将它设置为指向当前正在处理的对象。
For example:
例如:
function bark()
{
print "{$this->Name} says Woof!\n";
}
Whenever you are inside a function of an object, PHP automatically sets the $this
variable contains that object. You do not need to do anything to have access to it.
无论何时在对象的函数中,PHP都会自动设置$this变量包含该对象。你不需要做任何事情来访问它。
In Normal English:
$this
is a pseudo-variable which is available when a method is called from within an object context. It 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是一个伪变量,当从对象上下文中调用方法时可用。它是对调用对象的引用(通常是方法所属的对象,但如果从辅助对象的上下文中静态地调用方法,则可能是另一个对象)
An example:
一个例子:
<?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();
?>
Output:
输出:
$this is defined (A)
$this is not defined.
$this is defined (B)
$this is not defined.
#2
2
To elaborate shamittomar:
阐述shamittomar:
$this is a handle to be able to refer to the current object where the call is done in. (so it basically points to itself. Say we have multiple objects of the same class, and we wanted to set it up with (different) data before we do the ultimate move: echo it. It would be hard to point to itself when you don't know the object name.
$这是一个句柄,可以引用调用所在的当前对象。(所以它基本上指向自己。假设我们有同一个类的多个对象,我们想在执行最终操作之前用(不同的)数据来设置它:echo它。如果不知道对象名,就很难指出它自己。
class SaySomething{
private $the_line;// the variable exists only in this class!
public function __construct($myline){
$this->the_line = $myline;
// see how it points to itself?
// would there be a variable in the global scope then it would be not possible to "setup"
// the object without it getting overwritten in the next setup.
}
//The function to echo the stuf. Can be callid by the "outside world"
public function say_it(){
echo $this->the_line;
$this->add_exclamation();//call the function add_exclamation in this class/object.
}
//This function can not be called by the outside world because it's private.
// The only way to call it is from inside this class. To point to this class and call the function:
// $this->add_exclamation();
private function add_exclamation(){
echo "!";
}
}
$obja = new SaySomething('my');
$objb = new SaySomething('sample');
$objc = new SaySomething('super');
$objd = new SaySomething('text');
//Mind: uptill nothing has been said, only the private variable $the_line has been set in the constructor.
$obja->say_it();
$objc->say_it();
$objb->say_it();
$objd->say_it();
To understand what a class and what an object is (they tend to be mixed up a lot...) watch this slideshow: http://www.slideshare.net/sebastian_bergmann/understanding-the-php-object-model
要了解什么是类,什么是对象(它们经常被混淆…),请查看这个幻灯片:http://www.slideshare.net/sebastian_bergmann/understanding- php-object-model