php - 在静态函数中使用 $this 失败

标签 php oop this static-methods

我有一个我想在其中使用 $this 的方法,但我得到的只是: fatal error :不在对象上下文中使用 $this。

我怎样才能让它工作?

public static function userNameAvailibility()
{
     $result = $this->getsomthin();
}

最佳答案

这是正确的方法

public static function userNameAvailibility()
{
     $result = self::getsomthin();
}

静态方法使用 self:: 而不是 $this->

参见: PHP Static Methods Tutorial 了解更多信息 :)

关于php - 在静态函数中使用 $this 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2286696/

相关文章:

javascript - 分配中的左侧无效, "this = xxx"

javascript - 将变量从 PHP 传递到 Javascript 图表

php - 每年的分数数组

php - 声明 Doctrine Embeddable 是否可为空

c# - 在C#中的重写方法中捕获异常

c++ - 如何在另一边创建重载 +

php - 谷歌分析第22条

c++ - C++虚函数原理

visual-studio - Visual Studio 2015 禁用快速操作 "remove this qualitfication"

javascript - JavaScript 中的 var that = this