Check if a class constant exists

后端 未结 4 1326
醉梦人生
醉梦人生 2021-02-06 20:43

How can I check if a constant is defined in a PHP class?

class Foo {
    const BAR = 1;
}

Is there something like property_exists()

4条回答
  •  无人共我
    2021-02-06 21:14

    Yes, just use the class name in front of the constant name:

    defined('SomeNamespace\SomeClass::CHECKED_CONSTANT');
    

    http://www.php.net/manual/en/function.defined.php#106287

提交回复
热议问题