Curiosity: Will too much autoload in codeigniter make application slower?

1.3k views Asked by At

I'm using php and codeigniter, It has autoload class. My first question is,

If I use autoload class to load all model will it make my application slower? Or is there no effect?

My second question :

Which one is better and faster, loading all model you need using autoload class, Or load only some models you need in a class function?
2

There are 2 answers

0
S. M. Shahinul Islam On BEST ANSWER

1) Autoload class will obviously make your application slower. because it uses php4 require function to load files. there are some hacks to use php 5 autoloading feature. Hope, new owner of codeigniter will add add support for autoloading.

2) It is best to use load specific models rather than autoload. in previous point I stated the reason behind this. basically it is good practice to load only the needed model, helper, library and assets. it assures that you use minimum time and memory.

0
Zalaboza On

I use autoload. and its working like a charm with no significant impact on loading time.

Method

Add this peace of Code on top of any libraries/models that you use in you CI autoload.php

example for me my config/autoload.php looks like

$autoload['libraries'] = array('database','door','acl','form_validation','notify');

and in libraries/Door.php i added

<?php//libraries/Door.php
function multi_auto_require($class) {
    #var_dump("requesting $class");
    if(stripos($class, 'CI') === FALSE && stripos($class, 'PEAR') === FALSE) {
        foreach (array('objects','core') as $folder){//array of folders to look inside
            if (is_file(APPPATH."{$folder}/{$class}.php")){
                include_once APPPATH."{$folder}/{$class}.php";
            }
        }
    }
}

spl_autoload_register('multi_auto_require');
class Door {

i added this snippet just above class Door{ in this way this snippet will run on every time codeigniter loads door library.

Testing and benchmarking now For benchmarking i tested this code in a page with 17 DB queries from 8 Autoloaded Objects from 2 different folder with array of 3 folders to look in.

Result using above mentioned method Vs include_once 'classlocation.php' for all project classes,

the average of 10 page refreshs for both methods where around 0.6sec; so you can see there is no significant difference between both methods.

yet although i didn't test it on pages that doesn't use all class's, yet im sure autoloading makes my CI life much better and i'm happy with it.