变量$this在PHP中是什么意思?
我一直在PHP中看到变量变量$this在PHP中是什么意思?,php,class,oop,this,Php,Class,Oop,This,我一直在PHP中看到变量$this,我不知道它的用途。我从未亲自使用过它 有人能告诉我变量$this在PHP中是如何工作的吗?它是对当前对象的引用,在面向对象代码中最常用 参考: 底漆: 例如: <?php class Person { public $name; function __construct( $name ) { $this->name = $name; } }; $jack = new Person('Jack'); e
$this
,我不知道它的用途。我从未亲自使用过它
有人能告诉我变量
$this
在PHP中是如何工作的吗?它是对当前对象的引用,在面向对象代码中最常用
- 参考:
- 底漆:
<?php
class Person {
public $name;
function __construct( $name ) {
$this->name = $name;
}
};
$jack = new Person('Jack');
echo $jack->name;
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
}
}
这是一种从类本身引用类实例的方法,与许多其他面向对象语言一样
从:
伪变量$this可用
当从内部调用方法时
对象上下文$这是一个参考
到调用对象(通常是
方法所属的对象,
但可能是另一个物体,如果
方法是从
二级对象的上下文)
它引用当前类的实例,如meder所述
看。在第一个示例中对其进行了解释。当您创建一个类时,您拥有(在许多情况下)实例变量和方法(也称为.functions)$这将访问这些实例变量,以便您的函数可以获取这些变量并执行它们所需的操作
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中的$this
变量的最佳方法是在各种上下文中对解释器进行测试:
因此,$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对象的引用,该对象包含一个变量数组
如果在普通类中的普通方法内调用$this
,$this
将返回该方法所属的对象(类)
如果上下文没有父对象,$this
可能未定义
php.net有一个大页面,讨论php面向对象编程以及$this
如何根据上下文进行操作。
我知道它的老问题,无论如何,这是关于$的另一个确切解释$此主要用于引用类的属性
例如:
<?php
class Person {
public $name;
function __construct( $name ) {
$this->name = $name;
}
};
$jack = new Person('Jack');
echo $jack->name;
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
$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/>";
}
}
?>
让我们看看如果不使用$this并尝试使用实例变量和
与以下代码段同名的构造函数参数
<?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;
?>
这是一个详细的解释。我希望这将帮助初学者。我会让它变得非常简单
首先,让我们创建一个类
<?php
class Class1
{
}
该属性只是一个简单的变量,但我们将其命名为property,因为它位于类内部
方法只是一个简单的函数,但我们说方法是因为它也在一个类中
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);
所以Class1
的所有内容都在$object1
中,除了Method1
,我不知道为什么方法在转储对象时不显示
现在,如果我们只想访问$property1
,该怎么办。它很简单,我们做var_dump($object1->property1)代码>,我们刚刚添加了->property1
,我们指向它
我们还可以访问Method1()
,我们做var\u 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());
这将在浏览器中打印字符串(15)“我是属性1”
现在,不要在Method1()中执行此操作
我们这样做
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());
通常,此关键字在类内使用,通常与成员函数一起访问当前对象的类(变量或函数)的非静态成员
此关键字前面应加$符号
对于此运算符,我们使用->符号
然而,$将引用特定实例的成员变量和函数
让我们举个例子来了解$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();
?>
输出:
我是钢铁侠
注:
静态变量充当全局变量,并在类的所有对象之间共享。非静态变量特定于创建它们的实例对象。请再详细说明一下您的代码片段完全相同,还是我遗漏了什么?@Demento:是的。我在第二个构造函数中使用了$this
修复了它。您介意解释一下为什么name=$name不允许我访问学生的名字吗?这对我来说毫无意义,马里奥,这是因为范围。函数$name
内部是Tom,但在函数外部,它没有任何值,因为它的范围仅限于函数的范围。在KillerHP OOP教程中看到了类似的示例代码:)很好且简单的解释@梅德尔·欧穆拉列夫解释得很好。只是想补充一点,$this可以被视为有助于访问pr的伪对象/变量
$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());
<?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();
?>