【发布时间】:2010-12-04 03:42:31
【问题描述】:
我一直在 PHP 中看到变量 $this,但我不知道它的用途。我从来没有亲自使用过它。
谁能告诉我变量$this在PHP中是如何工作的?
【问题讨论】:
我一直在 PHP 中看到变量 $this,但我不知道它的用途。我从来没有亲自使用过它。
谁能告诉我变量$this在PHP中是如何工作的?
【问题讨论】:
它是对当前对象的引用,最常用于面向对象的代码中。
例子:
<?php
class Person {
public $name;
function __construct( $name ) {
$this->name = $name;
}
};
$jack = new Person('Jack');
echo $jack->name;
这会将“Jack”字符串存储为所创建对象的属性。
【讨论】:
$this 变量的最佳方法是在各种上下文中对解释器进行尝试: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 返回该方法所属的对象(类)。
如果上下文没有父对象,$this 可能未定义。
php.net 有一个大页面讨论 PHP 面向对象编程以及 $this 如何根据上下文运行。
https://www.php.net/manual/en/language.oop5.basic.php
【讨论】:
我知道它的老问题,无论如何关于 $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
【讨论】:
这是从自身内部引用类实例的方式,与许多其他面向对象的语言相同。
来自PHP docs:
伪变量 $this 可用 当从内部调用方法时 对象上下文。 $this 是一个参考 到调用对象(通常是 方法所属的对象, 但可能是另一个对象,如果 方法是从静态调用的 次要对象的上下文)。
【讨论】:
让我们看看如果我们不使用 $this 并尝试使用实例变量和 具有相同名称的构造函数参数,代码如下:sn-p
<?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;
?>
这与“汤姆”相呼应
【讨论】:
$this。
$name是Tom,但是在函数外部,它没有任何值,因为它的作用域仅限于函数的范围。
当你创建一个类时,你(在许多情况下)拥有实例变量和方法(又名函数)。 $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
【讨论】:
这是很长的详细解释。我希望这对初学者有帮助。我会让它变得非常简单。
首先,让我们创建一个类
<?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 及其所有内容的副本。我们使用var_dump() 转储了$object1 的所有内容。
这会给你
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());。
现在假设我想从Method1() 内部访问$property1,我会这样做
<?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 的新副本,或者我们可以说是一个实例。然后我们从$object2指向property1
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
【讨论】:
$this 是a reference to the calling object(通常是该方法所属的对象,但也可能是另一个对象,如果该方法是从辅助对象的上下文中静态调用的)。
【讨论】:
$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/>";
}
}
?>
【讨论】:
它指的是当前类的实例,正如meder所说。
请参阅PHP Docs。在第一个例子中已经解释过了。
【讨论】:
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();
?>
输出: 我是钢铁侠
注意: 静态变量充当全局变量,并在类的所有对象之间共享。非静态变量特定于创建它们的实例对象。
【讨论】: