模型Yii2与ReflectionClass不工作

时间:2015-07-07 15:27:55

标签: 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;

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

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

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

1 个答案:

答案 0 :(得分:8)

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

相关问题