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

Solved Why would class TextFormat not exist?

Comments in 'Plugin Development' started by Legoboy0215, Nov 7, 2015.

  1. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Why doesn't this work?
    PHP:
    use pocketmine\utils\TextFormat;

    $color "green";
    var_dump(constant("TextFormat::" strtoupper($color)));
    Error:
    Code:
     ClassNotFoundException: "Class TextFormat not found" (EXCEPTION) in "/src/spl/BaseClassLoader" at line 144 
  2. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Try putting the full class path of TextFormat. Instead of putting just "TextFormat", try putting "pocketmine\utils\TextFormat", and see if you still get that error.
    Ake, hoyinm14mc and Legoboy0215 like this.
  3. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Thanks! Works! But why? I want to know why :p
  4. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,130
    Plugins:
    4
    Minecraft User:
    iksaku
    Because the declaration of classes, in this problem using "Namespaces" fixes the problem, as the processor will gather it as a "class reference" instead of a simple string
  5. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    Hey, @iksaku is back! (again)
    Ake and zombie_Power like this.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    If you want to use imports, try this:
    PHP:
    constant(TextFormat::class . "::BLUE");
    Note that `use` is something that is run at compile time, replacing all type references of that type in your code into the full class reference.
    "TextFormat" is a string. According to php.net, all use statements are interpreted at compile time, where the compiler would not know that you are trying to use TextFormat as a class reference. Or do you want your messages sent to a player "I am a message with the word TextFormat inside" get changed for no reason at all?
    In PHP string literals, direct usage of backslashes has to be escaped. So you have to use "\\" instead. Although it currently works without errors, it is wrong programming practice (for instance, if you use "pocketmine\nbt", it will convert "\n" into a linefeed). Furthermore, according to PHP 7 Backwards-Incompatible Changes:
    This means that using \u incorrectly will result in an error, which applies directly to you. PHP 7 or not, it is wrong to try to use \ as a backslash in a string literal anyway.

    If you use single quotes, you can use backslashes without escaping, as long as the backslash isn't followed by a ' directly.
    Legoboy0215, Ake and Gamecrafter like this.

Share This Page

Advertisement