I would like to know…can I load the model in my constructor function instead of loading it for each function within that controller?
Lets say that I have 5 functions that use same model. Instad of calling the same model in all 5 functions, should I load the controller in the construcor function like this?
Is this good practice or no??
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class BackOfficeUsers extends MY_Controller
{
// constructor function
public function __construct()
{
parent::__construct();
$this->load->model('backOfficeUsersModel'); // load it here??
} // end of constructor function
Regards,Zoran
Yes, DRY is always a good practice.
You can also use CodeIgniter’s Autoloading feature. If you find yourself loading this model too often in other controllers.