Welcome Guest, Not a member yet? Register   Sign In
is it a good practice to load the model in the constructor function??
#1

[eluser]zoreli[/eluser]
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 loadin the same model in all 5 functions, should I load the controller in the construcor function like this?

Code:
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

Is this good practice or no??
#2

[eluser]GrahamDj28[/eluser]
Resources that I need throughout a controller, model, or whatever type of class, I load in the constructor. I do first define these variables above the constructor as
Code:
public, protected or private $variable_name;

Loading to much in the constructor can slow things down a bit
#3

[eluser]EzraBynx[/eluser]
I think this is a convenience versus performance issue. If the model is going to be used by most of your controller I would suggest loading the model in the constructor because it offers simplicity. I also feel as though it improves the readability of the code.

Loading to many unneeded resources in the constructor comes at a performance hit.
#4

[eluser]jmadsen[/eluser]
[quote author="EzraBynx" date="1349554444"]I think this is a convenience versus performance issue. If the model is going to be used by most of your controller I would suggest loading the model in the constructor because it offers simplicity. I also feel as though it improves the readability of the code.

Loading to many unneeded resources in the constructor comes at a performance hit.[/quote]

I don't think that's an issue - libraries (which included models) are loaded once, so there would be a brief uptick the first time that controller was hit is all.

I realize it's not quite that simple, but I personally don't concern myself with the performance part of this unless I'm building something that is going to get really pounded. Not that you should ignore best practices, but I would focus on other things to get your best performance (your db sql & interactions, for example)

Keep your code simple and easy to work with




Theme © iAndrew 2016 - Forum software by © MyBB