Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中获取类常量名称?_Php_Oop_Class Constants - Fatal编程技术网

在php中获取类常量名称?

在php中获取类常量名称?,php,oop,class-constants,Php,Oop,Class Constants,我有一个php类,其中包含一些指示实例状态的类常量 在使用该类时,在对其运行一些方法后,我会进行一些检查,以确保其状态与我期望的状态相同 例如,在调用一些方法之后,我希望状态是有意义的\u status\u NAME $objInstance->method1(); $objInstance->method2(); if ( $objInstance->status !== class::MEANINGFUL_STATUS_NAME ) { throw new Ex

我有一个php类,其中包含一些指示实例状态的类常量

在使用该类时,在对其运行一些方法后,我会进行一些检查,以确保其状态与我期望的状态相同

例如,在调用一些方法之后,我希望状态是
有意义的\u status\u NAME

$objInstance->method1();
$objInstance->method2();
if ( $objInstance->status !==  class::MEANINGFUL_STATUS_NAME ) { 
    throw new Exception("Status is wrong, should not be " . class::MEANINGFUL_STATUS_NAME . ".");
}
但是,这给了我异常消息

"Status is wrong, should not be 2"
当我真正想看到的是

"Status is wrong, should not be MEANINGFUL_STATUS_NAME"

所以我已经失去了常量名称的意义。我正在考虑制作一个“translation table”数组,这样我就可以将常量值转换回它们的名称,但这似乎很麻烦。我应该如何将其翻译回去,以便得到一条错误消息,让我更好地了解出了什么问题?

我现在想到,我可以使用字符串作为常量的值。我习惯于看数字。有什么原因我不应该这样做,或者为什么这样做不起作用吗?

这是一种棘手的解决方案:

$r = new ReflectionClass("YourClassName");
$constantNames = array_flip($r->getConstants());

$objInstance->method1();   
$objInstance->method2();   
if ( $objInstance->status !== YourClassName::MEANINGFUL_STATUS_NAME ) {    
    throw new Exception("Status is wrong, should not be " . $constantNames[YourClassName::MEANINGFUL_STATUS_NAME] . ".");   
} 

另一种方法可能是让对象检查状态是否处于所需模式

如果不是,对象的方法可以抛出异常

未经测试的示例: 学分:
考虑不使用反射可能是好的。 因为抛出的消息留在类中,
这样做很可能不会失去很多可维护性。

这或多或少是重复您将答案标记为解决方案的可能副本,但。。。你的答案是一个问题:)@vectorialpx我宁愿说这个答案引发了一个新的问题,超出了原来的范围。
class Klasse
{
    const WANTED_VALUE    =  1;
    const UNWANTED_VALUE  =  2;

    private static $constant_names  =  array();
    p... function __construct ()
    {
        if ( empty( self::$constant_names ) )
        {
            $class            =  new ReflectionClass( __CLASS__ );
            $constants        =  $class->getConstants();
            $constants        =  array_flip( $constants );// requires the constant's values to be unique
            self::$constants  =  $constants;
        }
    }
    public function checkNeededStatus ()
    {
        $needed_status  =  self::WANTED_VALUE;// could also be a parameter(an argument) with a default value
        if ( $this->status !== $needed_status )
        {
            $constant_name  =  self::$constants[ $this->status ];
            $message        =  'Status is wrong, '
                . 'should not be: `' . $constant_name . '`.'
            ;
            //$message .=  '(it should be `' . self::$constants[ $needed_status ] . '`)';
            throw new Exception( $message );
        }
    }
}

$objInstance  =  new Klasse();
$objInstance->method1();
$objInstance->method2();
$objInstance->checkNeededStatus();