CodeIgniter Forums
[DESIGN] same function in two controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: [DESIGN] same function in two controller (/thread-52410.html)



[DESIGN] same function in two controller - El Forum - 06-09-2012

[eluser]Zulkifli Said[/eluser]
hi guys,
i have two controllers in my CI apps.

- admin_controller.php, and
- user_controller.php

in user_controller.php, there is a function show_message() like this:

Code:
class User_controller extends Controller{

function show_message(){
  echo "your message";
}

}

and, in admin_controller.php, i want to write a function like show_message().

my question, what is the best way to use one function in two controller?


- Is i rewrite function show_message in admin_controller.php like this ?

class Admin_controller extends Controller{

Code:
function show_message(){
  echo "your message";
}

}

or

- i create that funciton in file helper and call the funtion in controller admin and user??


thanks...


[DESIGN] same function in two controller - El Forum - 06-09-2012

[eluser]InsiteFX[/eluser]
DRY create a MY_Controller and place the function in it, then you extend all of your controllers from it.

CodeIgniter Base Classes: Keeping it DRY