Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model Yii2 with ReflectionClass not working

Tags:

php

yii2

I have code - working correct (I do not have to inculde class 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 use ReflectionClass in code Yii2 i gets the message

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

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

like image 639
Rafal Avatar asked Jan 08 '23 00:01

Rafal


1 Answers

Because yii2 use namespaces, when you call new ReflectionClass() php looking for this class in the namespace that you declare at the beginnig of a file, in your case its namespace common\models; To load php's classes you need to prepend their names with \. So to instantiate ReflectionClass you need to write new \ReflectionClass(__CLASS__). More in documentation

like image 128
Tony Avatar answered Jan 14 '23 20:01

Tony