Creating Model By Holding Name In String $Var

Hello,

I try to create a model instance using string variable to store his name:




use common\models\User;   //  <<< !!!

// ----------


$model_name = 'User';

$model = $model_name::find()->all(); //  PHP Fatal Error 'yii\base\ErrorException' with message 'Class 'User' not found'




But next code works fine:




$model = User::find()->all();



ok, I changed the code to:




$model_name = 'common\models\User';



and now, code works fine.

But, why works -


User::find()->all();

and don’t worked -


$model_name = 'User';  $model_name::find()->all();

Thanks!

my question is why would you do that in the first place?

http://php.net/manual/en/language.namespaces.importing.php

[EDIT]




User::find()->all();



In compile-time, it gets compiled as




common\models\User::find()->all();



But a string variable will not be "compiled". It just gets interpreted in run-time.




$model_name = 'User';

$model_name::find()->all();

// same as User::find()->all() ... tries to call the global User::find()->all()



@softark, Thanks