Get value of dynamically chosen class constant in PHP

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

    Helper function

    You can use a function like this:

    function class_constant($class, $constant)
    {
        if ( ! is_string($class)) {
            $class = get_class($class);
        }
    
        return constant($class . '::' . $constant);
    }
    

    It takes two arguments:

    • Class name or object instance
    • Class constant name

    If an object instance is passed, its class name is inferred. If you use PHP 7, you can use ::class to pass appropriate class name without having to think about namespaces.

    Examples

    class MyClass
    {
        const MY_CONSTANT = 'value';
    }
    
    class_constant('MyClass', 'MY_CONSTANT'); # 'value'
    class_constant(MyClass::class, 'MY_CONSTANT'); # 'value' (PHP 7 only)
    
    $myInstance = new MyClass;
    class_constant($myInstance, 'MY_CONSTANT'); # 'value'
    
    0 讨论(0)
  • 2020-11-29 04:15

    If you have a reference to the class itself then you can do the following:

    if (defined(get_class($course). '::COURSES_PER_INSTANCE')) {
       // class constant is defined
    }
    
    0 讨论(0)
  • 2020-11-29 04:16

    I know I'm a bit late, but I hope this can help anyway.

    Based on Phil's answer, I created a default enumerator class that can be extended.

    class DefaultEnum
    {
        static public function getConstantText(string $constant)
        {
            try {
                // Get child class name that called this method
                $child_class = get_called_class();
    
                $reflection = new ReflectionClass($child_class);
                $const = $reflection->getConstant($constant);
    
                return $const;
            } catch (\ReflectionException $e) {
                // ...
            }
        }
    }
    
    class CustomEnum extends DefaultEnum
    {
        const something = 'abcd';
        const something2 = 'ABCD';
    }
    

    You can call this method like this

    CustomEnum::getConstantText('something');
    

    It will return 'abcd'.

    The function get_called_class() is a function that returns the class name that called this method and it works specifically for static methods.

    In this case $child_class value will be CustomEnum::class. ReflectionClass accepts strings and object as parameter.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-11-29 04:22

    Use Reflection

    $r = new ReflectionClass('ThingIDs');
    $id = $r->getConstant($thing);
    
    0 讨论(0)
  • 2020-11-29 04:22
    <?php
    
    class Dude {
        const TEST = 'howdy';
    }
    
    function symbol_to_value($symbol, $class){
        $refl = new ReflectionClass($class);
        $enum = $refl->getConstants();
        return isset($enum[$symbol])?$enum[$symbol]:false;
    }
    
    // print 'howdy'
    echo symbol_to_value('TEST', 'Dude');
    
    0 讨论(0)
提交回复
热议问题