Categories
symfony

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! 😛