在静态函数中使用$ this失败 [英] Using $this inside a static function fails

查看:69
本文介绍了在静态函数中使用$ this失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种我想在其中使用$ this的方法,但我得到的是:致命错误:不在对象上下文中时使用$ this.

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.

如何使它正常工作?

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

推荐答案

这是正确的方法

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

对于静态方法,使用self::代替$this->.

请参见: PHP静态方法教程 更多信息:)

See: PHP Static Methods Tutorial for more info :)

这篇关于在静态函数中使用$ this失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆