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

Multiple PHP files in one plugin

Comments in 'Plugin Development' started by MyNameIsTriXz, Dec 12, 2015.

  1. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Is there anything I need to do, like construct or need i do nothing just create a new file with everything like use poc... and class main extends...?
  2. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    When creating a class you have to construct the main class:
    PHP:
    use yourplugin\MainClass;

    Class 
    SecondClass {
      Public 
    $plugin;

      Public function 
    __construct(MainClass $main) {
        
    $this->plugin $main;
      }

      Public function 
    getPlugin() { // This function can be used to call methods from the main class. It is optional, u can just use $this->plugin directly.
        
    return $this->plugin;
      }
    }
    Now if u wanted to use methods from the second class in your main class:
    PHP:
    Use yourplugin\SecondClass;

    class 
    MainClass extends PluginBase {
      
      public function 
    getSecondClass() { // Optional functio . You could just use 'new SecondClass($this); directly
        
    Return new SecondClass($this);
      }
      
      
    // Public function onJoin(PlayerJoinEvent $event) {
          
    $this->getSecondClass()->yourMethodHere();
    }
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    The problem is, why do you want to create another class (the concept is another class, not another file)? Creating multiple classes is an actual practice in object-oriented programming, not merely to make the files look neater.
    HotFireyDeath and Gamecrafter like this.

Share This Page

Advertisement