tags:

views:

16

answers:

1

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 accomplish this?

+1  A: 

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

$model = 'Admin_Model_' . $type;
$db = new $model();
Jacob Relkin
It still doesn't work. I wrote `$this->db = new {'Admin_Model_' . $this->type}();` and received `Parse error: syntax error, unexpected '{' `
Rodrigo Alves
@Rodrigo, I updated my answer.
Jacob Relkin
right! just waiting to be able to accept the answer
Rodrigo Alves