Yii2 model with ReflectionClass not working

I have the code - works correctly (I don't need to inject the 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;

      

But, when I try to use ReflectionClass in Yii2 code, I get the message

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

      

If the framework has any Reflection classes or a way to declare ReflectionClass in Yii2

+3


source to share


1 answer


Since yii2 uses namespaces when you call new ReflectionClass()

php looking for that class in the namespace you declare at the beginning of the file, in your case its namespace common\models;

. To load php classes you need to add their names with \. So to create a ReflectionClass you need to write new \ReflectionClass(__CLASS__)

. Read more in the documentation



+8


source







All Articles