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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With