CodeIgniter Forums
using a variable to represent a class? Can anyone help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: using a variable to represent a class? Can anyone help (/showthread.php?tid=33089)



using a variable to represent a class? Can anyone help - El Forum - 08-15-2010

[eluser]prestondocks[/eluser]
Hi, Can anyone enlighten me on how to set a property in a class, where the class name is held in a variable.

I am trying to set a class property based on the value of class name in $this->router->class. I have tried $this->router->class::view_data['site_name'] = 'xyz' but as I expected I get a php error

The exact error being
Code:
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in C:\xampp\htdocs\intranet\system\application\modules\site\models\site_model.php on line 7

Can anyone help me out.

Thanks
Simon


using a variable to represent a class? Can anyone help - El Forum - 08-15-2010

[eluser]WanWizard[/eluser]
How about
Code:
$class = $this->router->class;
$this->$class->property = 'xyz';



using a variable to represent a class? Can anyone help - El Forum - 08-16-2010

[eluser]megablue[/eluser]
[quote author="WanWizard" date="1281926442"]How about
Code:
$class = $this->router->class;
$this->$class->property = 'xyz';
[/quote]

I'm using this way as well. You can not call an object or method dynamically by using an object or class statement. It will confuse the parser.


using a variable to represent a class? Can anyone help - El Forum - 08-16-2010

[eluser]prestondocks[/eluser]
Thanks for your input. I have not tried this solution yet, but will put it in my tool box and sure it will come in handy at some point.

I listened to the Codeigniter podcast last night issue 3. They were talking about sub classing and I realised this was the best way to go. I will create a sub class that extends controller which will contain the variable and I will make all my controllers extend this sub class.

This will give all my controllers access to that variable and will open up a lot of other options for sharing common data within my application. Hoping it will allow me reduce my code in regards to security and theming.

Thanks