【问题标题】:Codeigniter: Missing argument 1 for Autoloader::autoloader()Codeigniter:Autoloader::autoloader() 缺少参数 1
【发布时间】:2013-08-15 12:16:33
【问题描述】:

我一直在尝试通过在配置文件下方添加自动加载功能来自动加载我的核心类,但没有结果。现在我正在尝试使用钩子让它工作,但不幸的是同样的负面结果。当我尝试在 foreach 中输出文件路径时,我收到以下路径:

application/core/CI_Utf8.phpapplication/core/CI_URI.phpapplication/core/CI_Router.phpapplication/core/CI_Output.phpapplication/core/CI_Security.phpapplication/core/CI_Input.phpapplication/core/CI_Lang.phpapplication/core/CI_Loader.phpapplication/core/CI_DB.phpapplication/core/CI_DB.php    

有人可以与我分享我做错了什么吗? 在此先感谢

    $hook['post_controller_constructor'][] = array(
    'class' => 'Autoloader',
    'function' => 'register',
    'filename' => 'Autoloader.php',
    'filepath' => 'hooks',
    'params' => array(APPPATH.'core/')
);

$hook['post_controller_constructor'][] = array(
    'class' => 'Autoloader',
    'function' => 'register',
    'filename' => 'Autoloader.php',
    'filepath' => 'hooks',
    'params' => array(APPPATH.'controllers/')
);

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
    class Autoloader {

        private $_include_paths = array();


        public function register(array $paths = array())
        {   
            $this->_include_paths = $paths;

            spl_autoload_register(array($this, 'autoloader'));
        }


        public function autoloader($class)
        {   
            foreach($this->_include_paths as $path)
            {
                $filepath = $path . $class . EXT;



                if(!class_exists($class, FALSE) AND is_file($filepath))
                {
                    include_once($filepath);
                    echo $filepath;
                    break;
                }       
            }

        }
    } 
    ?>

【问题讨论】:

  • $this 关键字在这里指向 AutoLoader 类,您需要 &get_instance 的 codeigniter 类并将您的东西加载到其中。

标签: php codeigniter autoloader


【解决方案1】:

试试二维数组...

$hook['post_controller_constructor'][] = array(
                            'class'    => '',
                            'function' => 'load_config',
                            'filename' => 'load_config.php',
                            'filepath' => 'hooks'
                            );

$hook['post_controller_constructor'][] = array(
                            'class'    => '',
                            'function' => 'load_shops',
                            'filename' => 'load_shops.php',
                            'filepath' => 'hooks'
                            );  

【讨论】:

  • 在 codeigniter 中还有其他方法可以自动加载类。 1>在应用程序/库下创建库。您可以使用 config/autoload.php 自动加载库
猜你喜欢
  • 2017-10-05
  • 1970-01-01
  • 2014-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多