CodeIgniter Forums
$this->load->... in __construc() performance - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: $this->load->... in __construc() performance (/thread-41714.html)



$this->load->... in __construc() performance - El Forum - 05-15-2011

[eluser]veledrom[/eluser]
Hi,

I have 5 methods in my controller class. Individualy, all methods have their own $this->load->library, helper, download...... Between them total 15. If I put all those 15 in __construct would it cause performance issues?

Thanks


$this->load->... in __construc() performance - El Forum - 05-15-2011

[eluser]InsiteFX[/eluser]
I would use a MY_Controller and place them in the Constructor.

InsiteFX


$this->load->... in __construc() performance - El Forum - 05-15-2011

[eluser]veledrom[/eluser]
As I am new in CI, I read about MY_Controller (here) to understand why you suggested me to use it instead for all those $this->load..... bit in its __contruct() but to be honest I didn't quiet understand why to use it.

What would it be difference between these two? I am really interested in performance side of it. Remember, there are many methods in the main class and these loads are not necessarily related to all methods. Some methods need some and others don't. In any cases they are loaded with this way. Would it slow my application down?

Code:
class Mysite extends CI_Controller {
   function __construct()
   {
      parent::__construct();

      $this->load->helper('url');
      $this->load->helper('form');
      $this->load->helper('captcha');
      $this->load->helper('download');
      $this->load->library('one');
      $this->load->library('another');
      $this->load->library('andother');
      $this->load->model('one');
      $this->load->model('two');
      $this->load->model('three');
   }

Code:
class Mysite extends MY_Controller {
   function __construct()
   {
      parent::__construct();

      $this->load->helper('url');
      $this->load->helper('form');
      $this->load->helper('captcha');
      $this->load->helper('download');
      $this->load->library('one');
      $this->load->library('another');
      $this->load->library('andother');
      $this->load->model('one');
      $this->load->model('two');
      $this->load->model('three');
   }



$this->load->... in __construc() performance - El Forum - 05-15-2011

[eluser]InsiteFX[/eluser]
Because if you need those in another controller then all you need to do is extend the new controller from the MY_Controller.

InsiteFX


$this->load->... in __construc() performance - El Forum - 05-16-2011

[eluser]veledrom[/eluser]
First of all, thanks for your help.

I think there is a misunderstanding here. I din't want to put them in different controller. I just want to find out would it slow my application if I put all those $->load... bits in __construct of class Mysite extends CI_Controller{ }. If it doesn't slow the application then there is no need to use another class, isn't there?


$this->load->... in __construc() performance - El Forum - 05-16-2011

[eluser]John_Betong_002[/eluser]
Take a look at the User Guide "Benchmarking" and let us know the results.
 


$this->load->... in __construc() performance - El Forum - 05-16-2011

[eluser]osci[/eluser]
If you would like to change something you would have to do that in all your controllers.
If you keep your common construct in a base controller you only have to make changes to 1 file.


$this->load->... in __construc() performance - El Forum - 05-16-2011

[eluser]veledrom[/eluser]
Looks like putting all $this-load.. bits in __construct doesn't slow the application.

Benchmark returns same value even if I put $this-load.. bits in different methods.

Code:
class Cache extends CI_Controller {
    
    public function __construct()
    {
        parent::__construct();
        
        
        $this->load->helper('url');
        $this->load->helper('database');
        $this->load->helper('form');
        $this->load->helper('download');
        $this->load->helper('captcha');
        .
        .
        .
        
        
        $this->load->model('cache_model');
        $this->load->model('cache_model2');
        $this->load->model('cache_model3');
        $this->load->model('cache_model4');
        .
        .
        .
        .
    }
    
    public function index()
    {
        $this->benchmark->mark('code_start');
        //sleep(5);
        $this->benchmark->mark('code_end');
        
        echo $this->benchmark->elapsed_time('code_start', 'code_end');
    }
}