Dynamic access to classes in Symfony

It’s about something about how to simply your life. E.g. sometimes you may want to store table names in your DB. The reasons why you may want it are different. E.g. I want to generate constants for reference tables and in order to do that I use the following approach:

$tables = sfDatabaseTablePeer::doSelect(new Criteria());
if ($tables) foreach($tables as $t) {
$object_name = sfInflector::camelize($t->getName().’Peer’);
$object = new $object_name;
$values = $object->doSelect(new Criteria());

So as you may see I create object dynamically. Thanks God that’s what PHP allows to do and the only thing we need is to make sure we generate correct class name. I must say somehow I’ve been trying to generate dynamically access to class peer attributes, e.g. ClassPeer::{$NAME} – and I was not able to make it working. I’ve made a few attempts and no luck.

So if someone has ever done that please, please let me know! 😛

5 replies on “Dynamic access to classes in Symfony”


Are you trying to access something like:


If so, i managed to make it work with:

$object=call_user_func(array($this->getModel().’Peer’, ‘retrieveByPK’),$this->getIdItem())


To access class peer attributes, you can do it this way :
constant(‘ClassPeer::’.$NAME) 😉


Leave a Reply

Your email address will not be published. Required fields are marked *