1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Whats this function's use?

Discussion in 'Development' started by Hipster, May 2, 2017.

  1. Hipster

    Hipster Zombie

    Messages:
    214
    PHP:
     public static function getInstance() : self{
            return 
    self::$instance;
        }
     
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    it's called a singleton design
    which often returns a class or null
    and people like to call it "evil"
    it often is meant to run on single instance and some standard require you to private constructor and clone etc...
    blah blah blah...
     
    corytortoise likes this.
  3. falk

    falk Slime Poggit Reviewer

    Messages:
    75
    GitHub:
    falkirks
    Singleton design means that only one instance of a class should exist in the system. It is by no means evil. However, doing it in PHP can lead to memory leaks in a long running system like PocketMine.
     
    SOFe, Jack Noordhuis and jasonwynn10 like this.
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Memory leak for what, if it's just the plugin main class?
     
  5. falk

    falk Slime Poggit Reviewer

    Messages:
    75
    GitHub:
    falkirks
    Honestly I have little idea, this is just something that I was told way back when. I think the idea is if a plugin is removed from memory and doesn't clean up the reference to itself, it won't be garbage collected. And then the plugin will maintain references to who knows what (players, entities, chunks, and etc...) which could lead to a memory leak.
     
    jasonwynn10 likes this.
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    I bet it won't get too bad unless you have a plugin that keeps renaming its main class, not to mention that rarely do people unload plugins without stopping the server.
    Mainly, it's much harder to create a memory leak from this than to avoid one in my opinion :p
     
  7. Hipster

    Hipster Zombie

    Messages:
    214
    Whatttt, i have a plugin which has 7.5k lines and stays in main class only, i didn't make other files, why is that bad?
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.