CodeIgniter Forums

Full Version: Autoload function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I have some doubts about autoload in CodeIgniter.
My first question is that currently, I use __autoload, it has been discontinued? Is not it good practice to use it?
I read about the spl_autoload_register, is it better to use it instead of __autoload?
Also, my other doubt is related to server demand when using autoload, does it load EVERYTHING as soon as the server is loaded or is it on demand?
Because if it loads everything, it would not be good to put many includes there, right?

Thanks a lot ! Blush
For auto loading of base controller classes in core folder.

Can be modified to load other folders as well.

PHP Code:
// -----------------------------------------------------------------------

/**
 * Our SPL Autoloader.
 * -----------------------------------------------------------------------
 *
 * Add to the bottom of your ./application/config/config.php file.
 * 
 * Autoload Classes that are in the ./application/core folder.
 * 
 * Used to autoload base controller classes etc;
 */
spl_autoload_register(function ($class) {

    if (
strpos($class'CI_') !== 0)
    {
        if (
is_readable(APPPATH.'core/'.$class.'.php'))
        {
            require_once(
APPPATH.'core/'.$class.'.php');
        }
    }

}); 
Is it a good practice to have all includes what I will need within the autoload?

Is it on demand?

Thanks!
(02-24-2019, 06:23 AM)Skinper Wrote: [ -> ]Hello, I have some doubts about autoload in CodeIgniter.
My first question is that currently, I use __autoload, it has been discontinued? Is not it good practice to use it?
I read about the spl_autoload_register, is it better to use it instead of __autoload?
Also, my other doubt is related to server demand when using autoload, does it load EVERYTHING as soon as the server is loaded or is it on demand?
Because if it loads everything, it would not be good to put many includes there, right?

Thanks a lot ! Blush

You should use spl_autoload_register instead of __autoload. __autoload has been DEPRECATED as of PHP 7.2.0. Relying on this feature is highly discouraged.

The registered autoload function loads only the requested file on demand.
What is the best way to make autoload incldues?
such as:
include_once (DIR_LIB. "interface / components / TinyMce.php");

Thanks a lot for help!
(02-25-2019, 10:19 AM)Skinper Wrote: [ -> ]What is the best way to make autoload incldues?
such as:
       include_once (DIR_LIB. "interface / components / TinyMce.php");

Thanks a lot for help!

"Best Way" is subjective and depends on a bunch of factors that require more time to explain that I have time for (Sorry  Blush ), but it mostly boils down to one or more of the following.
  • A full path like you use above
  • A custom autoload function attached to the system by spl_autoload_register
  • A PSR-4 autoloader of the type usually made available to components installed with Composer
Both the second and third option require a logical file system structure and/or using namespaces and "use" commands in your PHP files.