Get value of dynamically chosen class constant in PHP

前端 未结 8 1158
无人共我
无人共我 2020-11-29 03:38

I would like to be able to do something like this:

class ThingIDs
{
    const Something = 1;
    const AnotherThing = 2;
}

$thing = \'Something\';
$id = Thi         


        
相关标签:
8条回答
  • 2020-11-29 04:25

    $id = constant("ThingIDs::$thing");

    http://php.net/manual/en/function.constant.php

    0 讨论(0)
  • 2020-11-29 04:27

    If you are using namespaces, you should include the namespace with the class.

    echo constant('My\Application\ThingClass::ThingConstant'); 
    
    0 讨论(0)
提交回复
热议问题