Why does the following code give me an exception saying that my constant isn't defined
MyClass::myFunction(MyClass::MY_CONST); // THIS GIVES THE ERROR
// This is the class..
class MyClass {
const MY_CONST = 'BLA';
public static function myFunction($key) {
if (!defined($key)) {
throw new Exception("$key is not defined as a constant");
}
}
}
I've tried with
if (!defined($key)) {}
if (!defined(self::$key)) {}
if (!defined(__CLASS__ . $key)) {}
You have to pass it as a string:
public static function myFunction($key) {
if (!defined('self::'.$key)) {
throw new Exception("$key is not defined as a constant");
}
}
MyClass::myFunction('MY_CONST');