CodeIgniter-Chinese / CodeIgniter

开源 PHP 框架 CodeIgniter 中国社区分支
http://codeigniter.org.cn/
MIT License
65 stars 26 forks source link

I can not understand the "helper" function #21

Open Daniel-ccx opened 10 years ago

Daniel-ccx commented 10 years ago

system/core/loader.php

I can not understand the "helper" function.

Why the "helper" has loaded "application/helper/test_func.php" and even try to check "system/helper/test_func.php" ?

There are too many "foreach" .

public function helper($helpers = array()) { foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper) { if (isset($this->_ci_helpers[$helper])) { continue; }

        // Is this a helper extension request?
        $ext_helper = config_item('subclass_prefix').$helper;
        $ext_loaded = FALSE;
        foreach ($this->_ci_helper_paths as $path)
        {
            if (file_exists($path.'helpers/'.$ext_helper.'.php'))
            {
                include_once($path.'helpers/'.$ext_helper.'.php');
                $ext_loaded = TRUE;
            }
        }

        // If we have loaded extensions - check if the base one is here
        if ($ext_loaded === TRUE)
        {
            $base_helper = BASEPATH.'helpers/'.$helper.'.php';
            if ( ! file_exists($base_helper))
            {
                show_error('Unable to load the requested file: helpers/'.$helper.'.php');
            }

            include_once($base_helper);
            $this->_ci_helpers[$helper] = TRUE;
            log_message('debug', 'Helper loaded: '.$helper);
            continue;
        }

        // No extensions found ... try loading regular helpers and/or overrides
        foreach ($this->_ci_helper_paths as $path)
        {
            if (file_exists($path.'helpers/'.$helper.'.php'))
            {
                include_once($path.'helpers/'.$helper.'.php');

                $this->_ci_helpers[$helper] = TRUE;
                log_message('debug', 'Helper loaded: '.$helper);
                break;
            }
        }

        // unable to load the helper
        if ( ! isset($this->_ci_helpers[$helper]))
        {
            show_error('Unable to load the requested file: helpers/'.$helper.'.php');
        }
    }

    return $this;
}