CodeIgniter Forums
getting a reference to code igniter super object - 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: getting a reference to code igniter super object (/showthread.php?tid=43200)



getting a reference to code igniter super object - El Forum - 07-04-2011

[eluser]nourdine[/eluser]
hello people ... I have an easy one here.

I noticed that in the docs it is recommended that we use get_instance() function to get an instance of the code igniter super object when we need to use its core libraries inside a custom lib. To be precise I noticed that the following snippet is used to get a reference to the code igniter context:

Code:
$CI =& get_instance();

I was wondering why you used
Code:
=&
instead of the simple
Code:
=
. The code igniter super object is clearly an object so it is passed by reference by default in php5 no? Can I assume I can simply use:

Code:
$CI = get_instance();

In case I am working with php5?

cheers


getting a reference to code igniter super object - El Forum - 07-04-2011

[eluser]C. Jiménez[/eluser]
because when you do &= you are referencing that object.

if you do = you get a copy.

if you work with a copy there, will be two objects in memory when you only need one,


getting a reference to code igniter super object - El Forum - 07-04-2011

[eluser]n0xie[/eluser]
[quote author="C. Jiménez" date="1309791978"]because when you do &= you are referencing that object.

if you do = you get a copy.
[/quote]
No no no.

PHP5 assigns by reference by default.

Quote:When assigning an already created instance of an object to a new variable, the new variable will access the same instance as the object that was assigned. This behaviour is the same when passing instances to a function. A new instance of an already created object can be made by cloning it.

The reason the examples use =& is because it's backwards compatible with PHP4. In PHP4 assigning an object would clone the object.


getting a reference to code igniter super object - El Forum - 07-04-2011

[eluser]nourdine[/eluser]
[quote author="n0xie" date="1309792898"][quote author="C. Jiménez" date="1309791978"]because when you do &= you are referencing that object.

if you do = you get a copy.
[/quote]
No no no.

PHP5 assigns by reference by default.

Quote:When assigning an already created instance of an object to a new variable, the new variable will access the same instance as the object that was assigned. This behaviour is the same when passing instances to a function. A new instance of an already created object can be made by cloning it.

The reason the examples use =& is because it's backwards compatible with PHP4. In PHP4 assigning an object would clone the object.[/quote]

Wink so am damn right aint i?


getting a reference to code igniter super object - El Forum - 07-04-2011

[eluser]C. Jiménez[/eluser]
I did'nt read "In case I am working with php5?".

Yeah, In specific case of php5 thats right.


getting a reference to code igniter super object - El Forum - 07-06-2011

[eluser]nourdine[/eluser]
thanks guys


getting a reference to code igniter super object - El Forum - 07-06-2011

[eluser]Crusoe[/eluser]
The server requirement for CI2 is PHP 5.1.16 or later, so actually, there is no need for backward compatibility here.