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

[eluser]global_erp_solution[/eluser]
I've encountered code
Code:
$CI =& get_instance();
$CI->session->set_userdata('logged', $user);

and this
Code:
$this->session->set_userdata('logged',$user);

what's the different effect between the two? when to use which? thanks
#2

[eluser]Sanjay Sarvaiya[/eluser]
as per my knowledge

you know CI is super object and we can get it using this syntax
Code:
$CI =& get_instance();
in helper file that contain only functions not class.

and this syntax
Code:
$this->session->set_userdata('logged',$user);
is use on controllers and views files,
$this is a super object of CI.

my presentation is not much enough to explain whole things.
#3

[eluser]global_erp_solution[/eluser]
so, they're both super objects? I'm confused
#4

[eluser]Sanjay Sarvaiya[/eluser]
yes
#5

[eluser]global_erp_solution[/eluser]
so, no difference whatsoever between the two? both are interchangeable? completely identical with no different effect?
#6

[eluser]LuckyFella73[/eluser]
There is a difference!

See User Guide for detailed information:
http://ellislab.com/codeigniter/user-gui...aries.html

Section: "Utilizing CodeIgniter Resources within Your Library"
#7

[eluser]Aken[/eluser]
$this->session is used in controllers and models. get_instance() is used everywhere else. Read more about PHP OOP and property scope to learn more about $this.
#8

[eluser]skunkbad[/eluser]
Aken is right.

CodeIgniter makes all loaded models and libraries available to the controller, which is why they are available using $this. $this refers to the controller instance, so when you are inside a controller, you are effectively accessing a property (sometimes called a member) that has been created for you by CI. You don't see it often in CI, but in many OOP implementations you will see "::". This is a scope resolution operator, and is usually used to call a static method, instead of "->", which is only used for accessing methods that are not static.
#9

[eluser]global_erp_solution[/eluser]
so, what's the different effect of using those two? and what does this have to do with LuckyFella73's creating libraries? any explanation? thanks
#10

[eluser]InsiteFX[/eluser]
Because with out the CI super object you cannot call the database or other libraries in a libaray

Create a library and try calling $this->session or $this->db

You will get an error.




Theme © iAndrew 2016 - Forum software by © MyBB