Nov 17
Digg
Stumbleupon
Technorati
Delicious

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


Author: symfonian

5 Comments

Pedro Trindade
November 18, 2007

Hi,

Are you trying to access something like:

$object=ClassPeer::retrieveByPK($id)

If so, i managed to make it work with:

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

Eric Lemoine
November 18, 2007

Hi

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

Eric

[…] Dynamic access to classes in Symfony […]

[…] Dynamic access to classes in Symfony […]

Grizvold
November 19, 2008

>>Eric Lemoine
>>November 18, 2007
Thanx so much, bro!
Your approach made my script working!

Comments RSS TrackBack Identifier URI

Leave a comment