Static functions cannot use $this
P粉277464743
P粉277464743 2023-10-17 17:54:29
0
2
535

I have this method and I want to use $this in it, but all I get is: Fatal error: $this is not used in an object context.

How can I make it work?

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


P粉277464743
P粉277464743

reply all (2)
P粉810050669

You cannot use$thisinside a static function because static functions are independent of any instantiated object. Try to make the function not static.

edit: By definition, static methods can be called without any instantiated object, so using$thisin a static method doesn't make any sense.

    P粉633075725

    This is the right thing to do

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

    Forstatic methods, useself::instead of$this->.

    See:PHP Static Method TutorialMore information:)

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!