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

Creating an API in a plugin

Discussion in 'Development' started by corytortoise, Mar 11, 2017.

  1. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    I've seen multiple ways that plugin developers create their own API in their plugins for others to use. Some create public static functions that return an instance of that plugin. Is this necessary? Is there a more effective way? I know you can use this
    PHP:
     $this->getServer()->getPluginManager()->getPlugin("myPluginName"); 
    to get a Plugin, but after that, can I use the retrieved plugin to run public functions?

    I ask this question because there doesn't seem to be any clear, preferred method for making an API. I want to create a good API with a plugin I am working on, but I don't know if I need to make a static instance getter in my plugin. (I recently saw quite a discussion about static functions, so I am a little uneasy about using them much.)

    I would appreciate any advice or guidance you can give, and thank you for your time.
     
  2. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Some plugins do use static getters, but it isn't exactly required to use the API of a plugin, using the way you showed in your post. Using the getPlugin() function will allow you to run the functions of your PluginBase class.

    Aside from that, an API doesn't necessarily just exist out of public functions. Think about creating custom events for example as well.
     
    corytortoise likes this.
  3. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    the static instance getter is called singleton which in some people opinion is evil
    and anything that is public means if another plugin can get your instance they can use it
     
    corytortoise likes this.
  4. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    So why do people use singleton getters? Is there any advantage? I just want to make my plugin as easy as possible to extend.
     
  5. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    i am not very sure, but as far as things goes singleton dosent make it easier to be extended
     
    SOFe likes this.
  6. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Okay, thanks.
     
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Forget all the talk about static methods. Someone who doesn't understand what APIs are started an illogical debate comparing performance of static methods in that thread, which is totally irrelevant to APIs. Just provide public ways to retrieve or change stuff in your plugin, and that's all an API is for.
     
  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.