CodeIgniter Forums
get_instance vs $GLOBALS - 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: get_instance vs $GLOBALS (/showthread.php?tid=42050)



get_instance vs $GLOBALS - El Forum - 05-24-2011

[eluser]korpirkor[/eluser]
Hi,
I have question about performance with using &get;_instance()

CodeIgniter is promoting using get_instance() all the time - in libraries, helpers etc
I was always thinking that accessing to a variable is faster than calling a function.

So, wouldn't it be faster call just once in Loader Class:
$GLOBALS['CI'] =& get_instance();
And after that just use $GLOBALS['CI'] ?

This way we don't even need to think: use $this or get_instance().

What do You think ?

Regards, Cezary Nowak


get_instance vs $GLOBALS - El Forum - 05-24-2011

[eluser]Pascal Kriete[/eluser]
In theory $CI is already a global variable:
Code:
global $CI;
$CI->load->helper('foo');

However, we do tweak the bootstrap code occasionally, so relying on certain global variables being present is probably a bad idea. In either case you're really optimizing the long tail. Refactoring to remove a file include or database call will more than make up for the performance lost from using get_instance().


get_instance vs $GLOBALS - El Forum - 05-24-2011

[eluser]John_Betong_002[/eluser]
 
 
http://dhorrigan.com/blog/article/codeigniter-and-object-references/
 
 


get_instance vs $GLOBALS - El Forum - 05-25-2011

[eluser]korpirkor[/eluser]
[quote author="Pascal Kriete" date="1306312327"]In either case you're really optimizing the long tail. Refactoring to remove a file include or database call will more than make up for the performance lost from using get_instance().[/quote]I'm aware of that Smile But now there are few frameworks faster than CI - yiiframework or DOOPHP... I think we shold think how to get lead :-)