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

Arrays in Classes

Comments in 'Plugin Development' started by Tethered_, Jun 26, 2015.

  1. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    I've got a class which has two private arrays that are empty, but are each initialized in the constructor. I was having some problems for some reason and decided to do a var_dump. When I did that, I noticed each array was empty except for the functions they were declared in. I'm assuming this has to do with public/private/static which I've always had a hard time with (even through AP Computer Science this year). Any ideas?
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Code?
  3. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    In PHP arrays are dynamic so they don't have a "real" initialization except if you put the elements when you declare an array (they are dynamic anyway)
    In languages like C#, Java and others...
    arrays must be initialized with the array size:
    Code:
    //C# and Java
    int[] myarray = new int[10];
    
    Then you can't add new elements to the array directly, you can only edit the existing elements.
    So in C# and Java you must use the List or Dictionary class.
    I don't understand your question well but if you can, post the code
    Last edited: Jun 26, 2015
    Jon likes this.
  4. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    I fixed it by changing the array variables to public static rather than private. Still not sure why it works and whether it is more/less secure, but it works
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    That means you are using classes incorrectly. @iksaku encountered this.
    Making it work isn't our goal. Making it work correctly is our goal. And it isn't related to static.
  6. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    I'll have to take a closer look at my code later tonight...
  7. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    Heres the code: http://pastebin.com/pU7eBxRs

    I commented out where the var_dumps work and don't. If someone could point me in the right direction to fix this that would be great.
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    The var is $this->isRunning? You didn't initialize it in __construct. Maybe onPlayerDeath got triggered before start gets called?
  9. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    Static var isRunning is initialized when the start method is used
  10. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    I'm having trouble with $alive and $infected
  11. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    Solved: I had registered this class as a listener in my main class, thus the static variable was the only one working because the event method was being called as a different instance of the class... This took me a day and a half to figure out...

Share This Page

Advertisement