Welcome Guest, Not a member yet? Register   Sign In
Difference in CI session
#11

[eluser]global_erp_solution[/eluser]
perhaps the discussion about library is still too advanced for me. But from what Aken said, I should use this->session when inside controller. Is this conclusion correct?
how about the tutorials that uses get_instance()->session in the controller? is that essentially the same, but a bad practice like when you call instance method using :: instead of ->?
#12

[eluser]LuckyFella73[/eluser]
Quote:perhaps the discussion about library is still too advanced for me

I don't think so - the need of get_instance() is not that hard to understand.

If you look at a controller or a model you see that the class extends the
CI_Controller (in controllers) or CI_Model (in models). By extending your
classes that way your class has direct access to the CI super object
using "$this".
In general: by extending a class with a second class all public method
of the second class are available to your class.

The CI libraries can't be extended that way (as far as I know) that's why
you need to create an instance of the CI super object that way. You assign
the CI super object to $CI (for example - you can choose your own name of course)
and from that point you can access CI classes via $CI->form_validation (or
whatever CI class you need).

Back to your last question: why should you get an instance of the super object,
assign it to a variable and access the CI objects via that variable if you
can access the CI objects directly?
Correct me if I'm wrong but I can't see an advantage doing that.

Hope to make it a little bit clearer
#13

[eluser]global_erp_solution[/eluser]
actually, I'm still trying to wrap my head around the two approaches used in tutorials I found on the web. I'm trying to confirm if they're actually pointing at the same object. If they do, then I can conclude that the two approaches are essentially the same, just different coding style. Correct me if I'm wrong. thanks
#14

[eluser]LuckyFella73[/eluser]
Taken from the User Guide:
Quote:Note: You'll notice that the above get_instance() function is being passed by reference:

$CI =& get_instance();

This is very important. Assigning by reference allows you to use the original CodeIgniter object rather than creating a copy of it.

Back to your question:
Quote:I’m trying to confirm if they’re actually pointing at the same object.
So the answer is: you are accessing the same object both ways.
#15

[eluser]Aken[/eluser]
[quote author="global_erp_solution" date="1343793445"]actually, I'm still trying to wrap my head around the two approaches used in tutorials I found on the web. I'm trying to confirm if they're actually pointing at the same object. If they do, then I can conclude that the two approaches are essentially the same, just different coding style. Correct me if I'm wrong. thanks[/quote]

They point at the same thing. It's not a "coding style", it depends on variable scope. $this is a reference to the object itself, or any parent it has extended. When you're within Controller or Model objects, they have already done the necessary code to make the CI object available to $this. When you create a library or other code/class outside of the CI object, you need to reference it using get_instance(), because $this will not be attached to it.

I highly suggest you read more about OOP PHP, and the variable scopes of objects. http://www.php.net/manual/en/language.oop5.basic.php
#16

[eluser]global_erp_solution[/eluser]
okay, thank you very much for the help. It clears things up.




Theme © iAndrew 2016 - Forum software by © MyBB