我想动态创建模型的实例。这可能与yii2?Yii2中动态声明模型
我想这样
<?php
namespace app\components;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\web\NotFoundHttpException;
use app\models;
class SintelComponent extends Component
{
public function find($model_name, $id)
{
$magic = __NAMESPACE__.'\\'.$model_name; //__NAMESPACE__ is a magic constant
if (($model = $magic::findOne($id)) !== null)
{
return $model;
}
else
{
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
$ MODEL_NAME东西是模型的名称。当我尝试这方面,我得到这样
语法错误,意外“$ MODEL_NAME”(T_VARIABLE)的错误,希望 标识符(T_STRING)
请参阅[名称空间常见问题](http://php.net/manual/en/language.namespaces.faq.php#language.namespaces.faq.quote) –