Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

self:: gives me a static error?

Comments in 'Plugin Development' started by Irish, Sep 26, 2016.

  1. Irish
    Offline

    Irish Active Member

    Joined:
    Apr 24, 2016
    Posts:
    96
    Minecraft User:
    _Irish_
    So I've been writing an API and when I get the instance of it, it says this.
    Code:
    [Server thread/CRITICAL]: Error: "Using $this when not in object context" (EXCEPTION) in "/plugins/PrankCore/src/PrankUHC/Scenarios/BaseScenarioAPI/BaseScenarioAPI" at line 36
    
    The line refers to the nonstatic getName() function and I'm sure it would do it with the rest of the parameters.
    PHP:
    public function getName()
        {
            return 
    $this->name;
        }
    But I'm sure its referring to when the instance is created. I do define the instance on construction, but it seems to make an instance anyways.
    Here is the function to get the instance.
    PHP:
    public static function getInstance(){
            if(
    self::$scenario == null){
                
    self::$scenario = new self(self::getName(), self::getDescription(), self::getAliases(), self::getConflictingScenarios());
            }
            return 
    self::$scenario;
        }

    The only reason I'm bewildered is because self isn't static in most cases. Any help would be appreciated, thanks for reading, Irish.
  2. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    $this cannot be used in static functions. When you call self::getName from getInstance (I don't even know how you did not trigger an error since getName isn't static), the getName function has $this in it.

Share This Page

Advertisement