模型Yii2与反射类不工作


Model Yii2 with ReflectionClass not working

我有代码-工作正确(我不需要包括类ReflectionClass):

class Test
{
    const TYPE_ONE = "Number one";
    const TYPE_TWO = "Number two";
    static function getConstants() { 
        $oClass = new ReflectionClass(__CLASS__);
        return $oClass->getConstants();
    }
}
   foreach (Test::getConstants() as $kay => $val):
   echo "$kay -- $val <br/>";
   endforeach;

但是,当我尝试在代码Yii2中使用ReflectionClass时,它得到消息

 PHP Fatal Error – yii'base'ErrorException
Class 'common'models'ReflectionClass' not found

如果框架中有任何反射类或在Yii2中声明ReflectionClass的方法

因为yii2使用命名空间,当你调用new ReflectionClass() php在你在文件开头声明的命名空间中寻找这个类时,在你的情况下,它的namespace common'models;加载php的类你需要用'前缀它们的名字。因此,要实例化ReflectionClass,您需要编写new 'ReflectionClass(__CLASS__)。更多文档