Using $this inside a static function fails

Jom picture Jom · Feb 18, 2010 · Viewed 65.4k times · Source

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.

How can I get this to work?

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

Answer

Sarfraz picture Sarfraz · Feb 18, 2010

This is the correct way

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

Use self:: instead of $this-> for static methods.

See: PHP Static Methods Tutorial for more info :)