Dynamic declaration of a model

hi,

i want to create an instance of a model dynamically. Is this possible with yii2?

i am trying something like this




public function find($model_name, $id) 

	{

		if (($model = \app\models\$model_name::findOne($id)) !== null) 

		{

            return $model;

        } 

        else 

        {

            throw new NotFoundHttpException('The requested page does not exist.');

        }

	}

$model_name is the name of the model. When i try this i get an error like this

syntax error, unexpected ‘$model_name’ (T_VARIABLE), expecting identifier (T_STRING)

Can PHP namespaces contain variables?

i tried this one

[color=black]<?[/color][color=black]php[/color][color=#00008B]namespace[/color][color=black] [/color][color=#2B91AF]App[/color][color=black]\login[/color][color=black];[/color][color=black] $p [/color][color=black]=[/color][color=black] [/color][color=maroon]‘login’[/color][color=black];[/color][color=black]$test2 [/color][color=black]=[/color][color=black] [/color][color=maroon]’\App\\’[/color][color=black].[/color][color=black]$p[/color][color=black].[/color][color=maroon]’\\MyClass’[/color][color=black];[/color][color=black]$test [/color][color=black]=[/color][color=black] [/color][color=#00008B]new[/color][color=black] $test2[/color][color=black];[/color]but it didn’t work in my case




<?php


namespace app\components;

 

use Yii;

use yii\base\Component;

use yii\base\InvalidConfigException; 

use yii\web\NotFoundHttpException;

//~ use app\models\$model_name;




use app\models; 

	

 

class SintelComponent extends Component

{

	public function find($model_name, $id) 

	{

		$org = 'app\models\\'.$model_name;

		

		$Organizations = new $org;//return $Organizations;

		if (($model =  $Organizations::className()->findOne($id)) !== null) 

		{

            return $model;

        } 

        else 

        {

            throw new NotFoundHttpException('The requested page does not exist.');

        }

	}

}