Get value of dynamically chosen class constant in PHP

前端 未结 8 1156
无人共我
无人共我 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:17

    My problem was similiar to this subject. When you have the object, but not the class name, you could use:

    $class_name = get_class($class_object);
    $class_const = 'My_Constant';
    
    $constant_value = constant($class_name.'::'.$class_const);
    

提交回复
热议问题