php 在静态函数中使用 $this 失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2286696/
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
Using $this inside a static function fails
提问by Jom
I have this method that I want to use $this in but all I get is: Fatal error: Using $this when not in object context.
我有一个我想在其中使用 $this 的方法,但我得到的只是:致命错误:不在对象上下文中使用 $this 。
How can I get this to work?
我怎样才能让它发挥作用?
public static function userNameAvailibility()
{
$result = $this->getsomthin();
}
回答by Sarfraz
This is the correct way
这是正确的方法
public static function userNameAvailibility()
{
$result = self::getsomthin();
}
Use self::instead of $this->for static methods.
使用self::,而不是$this->用于静态方法。
See: PHP Static Methods Tutorialfor more info :)
有关更多信息,请参阅:PHP 静态方法教程:)
回答by catchmeifyoutry
You can't use $thisinside a static function, because static functions are independent of any instantiated object.
Try making the function not static.
您不能$this在静态函数内部使用,因为静态函数独立于任何实例化对象。尝试使函数不是静态的。
Edit:
By definition, static methods can be called without any instantiated object, and thus there is no meaningful use of $thisinside a static method.
编辑:根据定义,可以在没有任何实例化对象的情况下调用静态方法,因此$this在静态方法内部没有有意义的使用。
回答by Gourav
Only static functions can be called within the static function using self:: if your class contains non static function which you want to use then you can declare the instance of the same class and use it.
只能使用 self:: 在静态函数中调用静态函数,如果您的类包含要使用的非静态函数,那么您可以声明同一个类的实例并使用它。
<?php
class some_class{
function nonStatic() {
//..... Some code ....
}
Static function isStatic(){
$someClassObject = new some_class;
$someClassObject->nonStatic();
}
}
?>
回答by Kangkan
The accessor thisrefers to the current instance of the class. As static methods does not run off the instance, using thisis barred. So one need to call the method directly here. The static method can not access anything in the scope of the instance, but access everything in the class scope outside instance scope.
访问器this引用类的当前实例。由于静态方法不会在实例之外运行,this因此禁止使用。所以这里需要直接调用该方法。静态方法不能访问实例范围内的任何内容,但可以访问实例范围之外的类范围内的所有内容。
回答by Midas Mtileni
It's a pity PHP doesn't show a descriptive enough error. You can not use $this-> inside a static function, but rather use self:: if you have to call a function inside the same class
遗憾的是 PHP 没有显示足够的描述性错误。你不能在静态函数中使用 $this-> ,而是使用 self:: 如果你必须在同一个类中调用一个函数
回答by Nicolai Nita
Here is an example of what happens when a method of a class is called in a wrong way. You will see some warnings when execute this code but it will work and will print: "I'm A: printing B property". (Executed in php5.6)
下面是一个示例,说明以错误的方式调用类的方法时会发生什么。执行此代码时您会看到一些警告,但它会起作用并打印:“我是 A:打印 B 属性”。(在php5.6中执行)
class A {
public function aMethod() {
echo "I'm A: ";
echo "printing " . $this->property;
}
}
class B {
public $property = "B property";
public function bMethod() {
A::aMethod();
}
}
$b = new B();
$b->bMethod();
It seams that the variable $this, used in a method which is called as a static method, points to the instance of the "caller" class. In the example above there is $this->property used in the A class which points to a property of the B.
它接缝在一个被称为静态方法的方法中使用的变量 $this 指向“调用者”类的实例。在上面的例子中,A 类中使用了 $this->property,它指向 B 的一个属性。
EDIT:
编辑:
The pseudo-variable $this is available when a method is called from within an object context. $this 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). PHP > The Basics
当从对象上下文中调用方法时,伪变量 $this 可用。$this 是对调用对象的引用(通常是该方法所属的对象,但也可能是另一个对象,如果该方法是从辅助对象的上下文中静态调用的)。 PHP > 基础知识
回答by Abbas Rahmati
In the static method,properties are for the class, not the object. This is why access to static methods or features is possible without creating an object. $this refers to an object made of a class, but $self only refers to the same class.
在静态方法中,属性是针对类的,而不是针对对象的。这就是为什么可以在不创建对象的情况下访问静态方法或功能的原因。$this 指的是一个由一个类组成的对象,而 $self 只指的是同一个类。

