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

Many classes

Discussion in 'Development' started by SergeyIvanov, Dec 24, 2016.

  1. SergeyIvanov

    SergeyIvanov Witch

    Messages:
    59
    GitHub:
    sergeyivanov14
    How to add 3 classes in 1 plugin?
     
  2. Dog2puppy

    Dog2puppy Slime

    Messages:
    94
    GitHub:
    colesquared
    Make different files for each class.
     
    applqpak likes this.
  3. Dinokiller

    Dinokiller Spider Jockey

    Messages:
    33
    MainClass.php:
    PHP:
    <?php

    namespace myplugin;

    use 
    pocketmine\plugin\PluginBase;

    class 
    MainClass extends PluginBase {

        public function 
    onEnable() {
            
    $foo = new SecondClass();
            
    $foo->doStuff();
            
    $bar = new ThirdClass();
            
    $bar->moreStuff();
        }

        public function 
    test() {
            
    // do stuff
        
    }
    }
    SecondClass.php:
    PHP:
    <?php

    namespace myplugin;

    class 
    SecondClass {

        public function 
    doStuff() {
            
    // do stuff
        
    }
    }
    ThirdClass.php:
    PHP:
    <?php

    namespace myplugin;

    class 
    ThirdClass {

        public function 
    moreStuff() {
            
    // do stuff
        
    }
    }
    All of these files should be in the same folder and the plugin.yml should only point to MainClass.
     
    [deleted] and applqpak like this.
  4. Primus

    Primus Zombie Pigman

    Messages:
    749
    According to PSR-0 and pocketmine documentation, class name MUST be identical to file name, excluding extension '.php' and namespace MUST be path relative to `src` folder. With that said, PocketMine will automatically load every class inside `src` folder and they can be accessed using the namespaces and imported using the `use` statements. Got it?

    An example: I've created a Foo.php inside src\bar folder and set the namespace to `bar` and now whenever I want to reference that class in any other file, I do it with namespace like this
    PHP:
    $foo = new bar\Foo();
    or
    PHP:
    use bar\Foo;

    $foo = new Foo();
    It's stupid that we have to repeat this over and over again. What makes you think that posting here will be more efficient? I haven't posted here anything related to my issues with PocketMine because most of them involve common sense and simple debugging techniques to resolve them or googling around to read about something new, like in your case this, so let me google that for you
     
  5. Dinokiller

    Dinokiller Spider Jockey

    Messages:
    33
    How the hell would he know to type PSR-0 into google? I've been doing PHP for four years and I've never even heard of that.
     
    applqpak and Primus like this.
  6. Dinokiller

    Dinokiller Spider Jockey

    Messages:
    33
    Not really....
     
    applqpak and Dog2puppy like this.
  7. Dog2puppy

    Dog2puppy Slime

    Messages:
    94
    GitHub:
    colesquared
    It's not a personal issue. I bet about 98% of the people here don't know what it means.
     
    applqpak likes this.
  8. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    Sometimes you should consider, Why you want multiple classes before how
     
  9. Jons

    Jons Spider

    Messages:
    14
    GitHub:
    jonsmc
    Sorry but i declare 2 classes in 1 file with both classes have different name than the file
     
  10. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    No, you don't need to.
    Yes, of course it works. PSR-0 is a code style. It suggest how to make clean and readable code.
     
    applqpak likes this.
  11. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    I've been doing PHP for almost a year (actually, "coding" for almost a year) and don't think PSR is a needed information to create a sub class :3
     
    Jons and applqpak like this.
  12. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    No, oft it is not needed to create a subclass. It is needed to create style conform code or contribute to pmmp. It will also help you reading your code later.
     
  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.