![]() |
Am I loading a class twice? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Am I loading a class twice? (/showthread.php?tid=34695) Pages:
1
2
|
Am I loading a class twice? - El Forum - 10-07-2010 [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()){ 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()){ 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? Am I loading a class twice? - El Forum - 10-07-2010 [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(){ 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/CodeIgniter-Base-Classes-Keeping-it-DRY Am I loading a class twice? - El Forum - 10-07-2010 [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(){ 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/CodeIgniter-Base-Classes-Keeping-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? Am I loading a class twice? - El Forum - 10-07-2010 [eluser]nuwanda[/eluser] Further, all I do in the constructor of every controller for admin pages is: Code: if(!$this->user->is_admin()){ Am I loading a class twice? - El Forum - 10-07-2010 [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()){ 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 Am I loading a class twice? - El Forum - 10-07-2010 [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()){ 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. Am I loading a class twice? - El Forum - 10-07-2010 [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()){ 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()){ 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(); if you are sure about the class being loaded anyway before that, you can ommit the second line Am I loading a class twice? - El Forum - 10-07-2010 [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. Am I loading a class twice? - El Forum - 10-07-2010 [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 Am I loading a class twice? - El Forum - 10-07-2010 [eluser]nuwanda[/eluser] Ok, seems like a matter of style. In my helper I have: Code: function profile_link($link_text='Profile'){ Which I use in my views like: Code: profile_link(); or with a parameter: Code: profile_link('Your Profile'); |