开发者

Concatenation while instantiating a class

开发者 https://www.devze.com 2023-01-16 07:26 出处:网络
I\'d like to instantiate a model (in Zend Framework) using a generic way. I have $type variable which contains the name of the model. I\'d like to write something like this

I'd like to instantiate a model (in Zend Framework) using a generic way. I have $type variable which contains the name of the model. I'd like to write something like this

$db = new Admin_Model_{$type}();

But it doesn't work, Is there any way to 开发者_C百科accomplish this?


You need to have the entire class name as a string in order for this to work:

$model = 'Admin_Model_' . $type;
$db = new $model();
0

精彩评论

暂无评论...
验证码 换一张
取 消