How could I add a configuration to a const? My current code is PHP: public const ERROR = $this->getConfig->get("error-message"); However it doesn't work. Prior to adding the config it was just: PHP: public const ERROR = "Error:"; What should I change for this to work?
You can't define class constants in runtime. PHP: public static $ERROR;static::$ERROR = $this->getConfig()->get('error-message'); At that moment when constant is assigned with its value, $this variable is not even initialized. Therefore getting value out of Config object is not possible.
Hi! PHP: // Making it.public const ERROR= $this->getConfig->get("error-message"); // You're trying to get plugin configuration, and in plugin configuration isn't the error I remind you using this code: PHP: // Before start, you need to use Config class:use pocketmine\utils\Config;// This should be in "onEnable" function, replacing the another config$this->getServer()->getPluginManager()->registerEvents($this, $this);$this->database = new Config($this->getDataFolder() . "stats.yml", Config::YAML, array( "last-error" = ""));$this->database;// When an error happens, you use this code to set error$this->database->set("last-error", "Error Message Here");// To get the error with a const, you use this:public const ERROR= $this->database->get("last-error"); With this, in plugin_data folder you will see a file named stats.yml, when an error happens, you will se a change in the file and it should be like this: Code: --- last-error: "Last Error Message" ... I hope this works for you, Thanks!
Yes, but your constant won't change values. And you can't redefine constants as they act like final static variables, for example