Welcome Guest, Not a member yet? Register   Sign In
getting a reference to code igniter super object
#1

[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
#2

[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,
#3

[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.
#4

[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?
#5

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

Yeah, In specific case of php5 thats right.
#6

[eluser]nourdine[/eluser]
thanks guys
#7

[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.




Theme © iAndrew 2016 - Forum software by © MyBB