PHP 类或 PHP 方法中的 self 和 $this-> 之间的区别在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1948315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where's the difference between self and $this-> in a PHP class or PHP method?
提问by openfrog
Where's the difference between selfand $this->in a PHP class or PHP method?
PHP 类或 PHP 方法之间self和$this->中的区别在哪里?
Example:
例子:
I've seen this code recently.
我最近看到了这段代码。
public static function getInstance() {
if (!self::$instance) {
self::$instance = new PDO("mysql:host='localhost';dbname='animals'", 'username', 'password');;
self::$instance-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return self::$instance;
}
But I remember that $this->refers to the current instance (object) of a class (might also be wrong). However, what's the difference?
但是我记得那个$this->是指一个类的当前实例(对象)(也可能是错误的)。然而,有什么区别呢?
回答by Tor Valamo
$thisrefers to the instance of the class, that is correct. However, there is also something called static state, which is the same for all instances of that class. self::is the accessor for those attributes and functions.
$this指的是类的实例,这是正确的。然而,还有一种叫做静态的东西,对于那个类的所有实例都是一样的。self::是这些属性和函数的访问器。
Also, you cannot normally access an instance member from a static method. Meaning, you cannot do
此外,您通常无法从静态方法访问实例成员。意思是,你不能做
static function something($x) {
$this->that = $x;
}
because the static method would not know which instance you are referring to.
因为静态方法不知道您指的是哪个实例。
回答by Yacoby
$thisrefers to the current object, selfrefers to the current class. The class is the blueprint of the object. So you define a class, but you construct objects.
$this指当前对象,self指当前类。类是对象的蓝图。所以你定义了一个类,但你构造了对象。
So in other words, use self for static and this for non-static members or methods.
因此,换句话说,将 self 用于静态,将 this 用于非静态成员或方法。
回答by Ahmed Magdy
- this-> can't access static method or static attribute , we use self to access them.
$this-> when dealing with extended class will refer to the current scope that u extended , self will always refer to the parent class because its doesn't need instance to access class method or attr its access the class directly.
<?php class FirstClass{ function selfTest(){ $this->classCheck(); self::classCheck(); } function classCheck(){ echo "First Class"; } } class SecondClass extends FirstClass{ function classCheck(){ echo "Second Class"; } } $var = new SecondClass(); $var->selfTest(); //this-> will refer to Second Class , where self refer to the parent class
- this-> 不能访问静态方法或静态属性,我们使用 self 来访问它们。
$this-> 在处理扩展类时将引用您扩展的当前范围,self 将始终引用父类,因为它不需要实例来访问类方法或直接访问类。
<?php class FirstClass{ function selfTest(){ $this->classCheck(); self::classCheck(); } function classCheck(){ echo "First Class"; } } class SecondClass extends FirstClass{ function classCheck(){ echo "Second Class"; } } $var = new SecondClass(); $var->selfTest(); //this-> will refer to Second Class , where self refer to the parent class
回答by jldupont
selfis used at the class-level scope whereas $thisis used at the instance-level scope.
self在类级别范围内$this使用,而在实例级别范围内使用。
回答by Pascal MARTIN
$thisis used to reference methods and properties of the current instance of a class.
$this用于引用类的当前实例的方法和属性。
selfus used to reference static methods and properties, shared by all instances (and even accessible outside of any instance)of a class.
self我们过去常常引用静态方法和属性,由类的所有实例(甚至可以在任何实例之外访问)共享。
You can take a look at Static Keyword(quoting a few lines):
你可以看看静态关键字(引用几行):
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can)
...
Static properties cannot be accessed through the object using the arrow operator ->.
将类属性或方法声明为静态使它们无需实例化即可访问。声明为静态的属性不能被实例化的类对象访问(尽管静态方法可以)
...
无法使用箭头运算符 -> 通过对象访问静态属性。
And, from the page Properties(quoting):
并且,从页面属性(引用):
Within class methods the properties, constants, and methods may be accessed by using the form
$this->property(wherepropertyis the name of the property)unless the access is to a static property within the context of a static class method, in which case it is accessed using the formself::$property.
在类方法中,可以使用表单
$this->property(其中property是属性的名称)访问属性、常量和方法,除非访问是在静态类方法的上下文中访问静态属性,在这种情况下,可以使用形式self::$property。
回答by Nimatullah Razmjo
$thisis use to call the instance of class, where self::is mostly used to call the constant variable within class.
$this用于调用类的实例,其中self::主要用于调用类内的常量变量。
回答by Matchu
selfrefers to the calling object's class. $thisrefers to the object itself.
self指的是调用对象的类。$this指对象本身。

