模型Yii2与ReflectionClass不工作

Raf*_*fal 4 php yii2

我有代码 - 工作正确(我不必包含类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;
Run Code Online (Sandbox Code Playgroud)

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

 PHP Fatal Error – yii\base\ErrorException
Class 'common\models\ReflectionClass' not found
Run Code Online (Sandbox Code Playgroud)

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

Ton*_*ony 8

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