CodeIgniter Forums
Problem working out how to make $CI->$variable work - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Problem working out how to make $CI->$variable work (/showthread.php?tid=33230)



Problem working out how to make $CI->$variable work - El Forum - 08-19-2010

[eluser]J Maxwell[/eluser]
I have searched a bit for this, but strugging to work out how to achieve what I want.

I am querying a database, then dynamically loading assets based on the results of the query.

However, I need to do something like this:


Code:
foreach ($w as $widget) :

/* i've written an extension to the CI_Loader that enables me to add widgets.
* It loads them into $this->widget_name however, because i need to access them during a
* foreach loop, then I can't manually enter the name of the widget in order to use it, it needs
* to come from the variable
*/
$CI->load->widget($widget);
$CI->$widget->do_stuff_here();

endforeach;

The problem with this is it keeps throwing member function on non-object errors. How do I refer to the widget I just created without being able to type the name in?


Cheers,

John


Problem working out how to make $CI->$variable work - El Forum - 08-19-2010

[eluser]WanWizard[/eluser]
Does your $this->load->widget() method assign the new class to the CI object?

Something simple as this would do it:
Code:
$CI =& get instance();
$CI->$widget = new $widget();



Problem working out how to make $CI->$variable work - El Forum - 08-19-2010

[eluser]J Maxwell[/eluser]
Hey, thanks for that, I was sure there was a pretty simple explanation and I'd spent long enough staring blankly at the screen that I wasn't going to get it!

Problem fixed, many thanks.

John