phpoopthisstatic-methods

Using $this inside a static function fails


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();
}

Solution

  • 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 :)