我想動態創建模型的實例。這可能與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) –