Answer the question
In order to leave comments, you need to log in
How to create object in Yii2 by name in variable?
Previously, in Yii1, you could create models like this:
$modelName = 'MyModel';
new $modelName;
use frontend\models\MyModel;
new $modelName;
Answer the question
In order to leave comments, you need to log in
Here Yii2 is not in business at all. This is the work with namespaces that you need to learn.
$string = '\\read\\the\\docs\\Php';
$obj1 = new $string;
$result = $string::someMethod();
same only with namespace
$modelName = '\frontend\models\MyModel';
new $modelName;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question