Welcome Guest, Not a member yet? Register   Sign In
Am I loading a class twice?
#7

[eluser]Buso[/eluser]
[quote author="nuwanda" date="1286450816"]I have a user class that I load in the constructor for every controller.

I use it like this in controllers and views:

Code:
if($this->user->is_logged_in()){
  do this
}

Works fine.

But I have a helper where I need to check logged in to output a link. I can't use the above user->method because it's not available to the helper.

So I call it statically, like:

Code:
if(user::is_logged_in()){
  do this
}

Works fine as well.

Now I know that calling the method statically doesn't instantiate the class, but is it wasteful to load the class in the constructor and then call it statically in the helper?[/quote]
If the helper needs the user class to be loaded (and you don't know if it is), you can use the loader class from within your helper:
Code:
$ci = get_instance();
$ci->load->library('user'); // won't load it again if already loaded, don't worry
$ci->user->is_logged_in() AND do_something();

if you are sure about the class being loaded anyway before that, you can ommit the second line


Messages In This Thread
Am I loading a class twice? - by El Forum - 10-07-2010, 12:26 AM
Am I loading a class twice? - by El Forum - 10-07-2010, 12:38 AM
Am I loading a class twice? - by El Forum - 10-07-2010, 01:02 AM
Am I loading a class twice? - by El Forum - 10-07-2010, 01:12 AM
Am I loading a class twice? - by El Forum - 10-07-2010, 01:18 AM
Am I loading a class twice? - by El Forum - 10-07-2010, 01:37 AM
Am I loading a class twice? - by El Forum - 10-07-2010, 01:47 AM
Am I loading a class twice? - by El Forum - 10-07-2010, 02:00 AM
Am I loading a class twice? - by El Forum - 10-07-2010, 02:15 AM
Am I loading a class twice? - by El Forum - 10-07-2010, 02:28 AM
Am I loading a class twice? - by El Forum - 10-07-2010, 02:31 AM
Am I loading a class twice? - by El Forum - 10-07-2010, 02:36 AM
Am I loading a class twice? - by El Forum - 10-07-2010, 02:42 AM
Am I loading a class twice? - by El Forum - 10-07-2010, 02:47 AM
Am I loading a class twice? - by El Forum - 10-07-2010, 02:48 AM
Am I loading a class twice? - by El Forum - 10-07-2010, 02:54 AM
Am I loading a class twice? - by El Forum - 10-07-2010, 02:57 AM
Am I loading a class twice? - by El Forum - 10-07-2010, 03:01 AM



Theme © iAndrew 2016 - Forum software by © MyBB