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

Namespace and Class Help

Comments in 'Plugin Development' started by Tethered_, May 31, 2015.

  1. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    I keep getting the error Class games\Spleef not found and I'm assuming it has to do with my usage of namespaces, which I'm terrible with. Here's my hierarchy of folders and files:

    Minigames:
    src:
    minigames:
    Main.php
    Timer.php
    BaseGame.php
    games:
    Spleef.php​

    Main.php:
    PHP:
    namespace minigames;

    use 
    games\Spleef//I think this is wrong, not sure though

    class Main extends PluginBase implements Listener {
    //blah blah blah
    }
    Spleef.php
    PHP:
    namespace games;

    class 
    Spleef implements BaseGameListener {
    //blah blah blah
    }
    Any help would be much appreciated
  2. luca28pet
    Offline

    luca28pet Active Member

    Joined:
    Feb 20, 2014
    Posts:
    251
    Minecraft User:
    luca28pet
    Try this Spleef.php (I changed the namespace) :
    PHP:
    namespace minigames\games;

    class 
    Spleef implements BaseGameListener {//blah blah blah}
    EvolSoft likes this.
  3. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    Still says games\Spleef not found
  4. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    I changed Main.php to
    PHP:
    use minigames\games\Spleef;
    and Spleef.php to
    PHP:
    namespace minigames\games;
    and it worked :D
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    In namespace and use statements, you always include the FULLY QUALIFIED name of the class/function/constant without a leading `\`.
    In normal code references, to include fully qualified name, the class name must start with a `\`. Otherwise, the class name will be assumed as relative (like relative file paths). Of course, if you are in root/global namespace, the `\` doesn't matter.

Share This Page

Advertisement