How to add autoload function in CodeIgniter?

I would like to be able to use OOP and create new objects in my controllers in CodeIgniter. Therefore, I need to use the autoload function:

function __autoload( $classname )
{
    require_once("../records/$classname.php");
} 

But how can I add this to CodeIgniter?

+5
source share
3 answers

You can add your autoloader above app/config/config.php. I used a similar function autoloadearlier in this place and it worked pretty neatly.

function __autoload($class)
{
    if (strpos($class, 'CI_') !== 0)
    {
        @include_once(APPPATH . 'core/' . $class . EXT);
    }
} 

. . core, , records ; , . CI_ libs ()

+9

" " .

, application/config/autoload.php , . , .

0

, .

config/config.php

$config['enable_hooks'] = TRUE;


/config/hooks.php "pre_system", core/CodeIgniter.php, .

$hook['pre_system'] = array(
    0 => array(         
        'function' => 'load_initial_functions',
        'filename' => 'your_hooks.php',
        'filepath' => 'hooks'
    )
);

hooks 2 :

-: application/hooks/your_functions.php __ autoload , .

: application/hooks/your_hooks.php :

function load_initial_functions()
{
    require_once(APPPATH.'hooks/your_functions.php');
}

, _functions.php, .

-2

All Articles