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

[eluser]nuwanda[/eluser]
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?
#2

[eluser]umefarooq[/eluser]
to call you libraries,models,views in you helper create a ci instance then you can call libraries

Code:
function test_helper(){
    $ci = & get_instance();
    $ci->load->library('user');
    $ci->user->is_logged_in();
  
  }

another thing don't load user class in each controller constructor just call once if you want to use in all controller read this article will help you

http://philsturgeon.co.uk/news/2010/02/C...ing-it-DRY
#3

[eluser]nuwanda[/eluser]
[quote author="umefarooq" date="1286451502"]to call you libraries,models,views in you helper create a ci instance then you can call libraries

Code:
function test_helper(){
    $ci = & get_instance();
    $ci->load->library('user');
    $ci->user->is_logged_in();
  
  }

another thing don't load user class in each controller constructor just call once if you want to use in all controller read this article will help you

http://philsturgeon.co.uk/news/2010/02/C...ing-it-DRY[/quote]

Phil's comments mention:

Quote:For any code that you want in EVERY controller, use MY_Controller. For code you want in SOME controllers, use the named method.

So I want the user class in every controller for site-wide calls. Surely loading it in MY_controller is correct?
#4

[eluser]nuwanda[/eluser]
Further, all I do in the constructor of every controller for admin pages is:

Code:
if(!$this->user->is_admin()){
  redirect to non-admin
}
#5

[eluser]umefarooq[/eluser]
[quote author="nuwanda" date="1286453542"]Further, all I do in the constructor of every controller for admin pages is:

Code:
if(!$this->user->is_admin()){
  redirect to non-admin
}
[/quote]

no need to put even this code in every controller constructor you can put in your MY_Controller or create one Admin_Controller and extend your all admin controller with this and put this code in your parent class constructor and it will be called first every time
#6

[eluser]nuwanda[/eluser]
[quote author="umefarooq" date="1286453909"][quote author="nuwanda" date="1286453542"]Further, all I do in the constructor of every controller for admin pages is:

Code:
if(!$this->user->is_admin()){
  redirect to non-admin
}
[/quote]

no need to put even this code in every controller constructor you can put in your MY_Controller or create one Admin_Controller and extend your all admin controller with this and put this code in your parent class constructor and it will be called first every time[/quote]

Sure, I get that.

I guess since that's about the only thing I'm doing for my admin controllers, it seems easier. If I was doing more setup it would make sense to have a discrete admin controller to inherit.

Thanks.
#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
#8

[eluser]nuwanda[/eluser]
Hi, Buso

The class is not loaded in the helper. If I reference it like $this->user->method it throws an error, so that's why I used a static call. I didn't see any point in setting up a load for it again.

I assume I can use a static call because CI already had a reference (maybe the wrong term) to the class because it was loaded in the controller. Else CI must be able to take static calls simply by virtue of the class being in the libraries dir.

My original question was is there any performance hit doing it that way. It works fine.
#9

[eluser]Buso[/eluser]
It wont be any performance hit, but the method (is_logged_in) could have referenced $this (the user instance) or an instance attribute and it wouldn't have worked, so the usual way is calling get_instance()->user->is_logged_in(), or using an extra helper is_logged_in(), which calls the above.

Even if it works now, the implementation of User::is_logged_in() could change, and after an update your code wouldn't be compatible with it anymore
#10

[eluser]nuwanda[/eluser]
Ok, seems like a matter of style.

In my helper I have:

Code:
function profile_link($link_text='Profile'){

  if(user::is_logged_in()){
    echo anchor('user/profile', $link_text);
  }

}

Which I use in my views like:

Code:
profile_link();

or with a parameter:

Code:
profile_link('Your Profile');




Theme © iAndrew 2016 - Forum software by © MyBB