Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CI 3中的延迟负载模型和库\uuu获取控制器的神奇方法_Php_Codeigniter - Fatal编程技术网

Php CI 3中的延迟负载模型和库\uuu获取控制器的神奇方法

Php CI 3中的延迟负载模型和库\uuu获取控制器的神奇方法,php,codeigniter,Php,Codeigniter,我正在尝试为CI 3.0.4中的模型+库编写一个延迟加载程序 我正在尝试获取以下内容,如中所述: 这段代码似乎适用于CI版本2,当我尝试将其应用于CI版本3.0.4时,出现以下错误 Severity: Notice Message: Indirect modification of overloaded property Items::$benchmark has no effect Filename: core/Controller.php Line Number: 75 Fata

我正在尝试为CI 3.0.4中的模型+库编写一个延迟加载程序

我正在尝试获取以下内容,如中所述:

这段代码似乎适用于CI版本2,当我尝试将其应用于CI版本3.0.4时,出现以下错误

Severity: Notice

Message: Indirect modification of overloaded property Items::$benchmark has no effect

Filename: core/Controller.php

Line Number: 75


Fatal error: Cannot assign by reference to overloaded object in /Applications/MAMP/htdocs/phppos/PHP-Point-Of-Sale/system/core/Controller.php on line 75
以下是我所做的:

  • 创建文件application/core/MY_Controller.php

  • 创建以下代码:

  • 一旦我能让它毫无差错地工作;我将添加自定义逻辑以加载正确的模型或库


    我希望在不修改核心的情况下完成此操作。

    基于示例,我提出了以下不需要核心修改且适用于CI 3.0.4的方法。它首先尝试加载模型,如果模型不存在,则尝试加载库

  • 创建文件application/core/MY_Controller.php

  • 把这些东西放进去

    <?php
    class MY_Controller extends CI_Controller 
    {
    
        //Lazy loading based on http://stackoverflow.com/questions/17579449/model-library-lazy-load-in-codeigniter
        public function __construct()
        {
            foreach (is_loaded() as $var => $class)
            {
                 $this->$var = '';
            }
    
            $this->load = '';
            parent::__construct();
        }
    
    
        // Lazy load models + libraries....If we can't load a model that we have; then we will try to load library $name
        public function __get($name)
        {
            //Cache models so we only scan model dir once
    
            static $models = FALSE;
            $this->load->helper('file');
    
            if (!$models)
            {
                $model_files = get_filenames(APPPATH.'models', TRUE);
                foreach($model_files as $model_file)
                {
                    $model_relative_name = str_replace('.php','',substr($model_file,strlen(APPPATH.'models'.DIRECTORY_SEPARATOR)));
                    $model_folder = strpos($model_relative_name, DIRECTORY_SEPARATOR) !== FALSE ? substr($model_relative_name,0,strrpos($model_relative_name,DIRECTORY_SEPARATOR)) : '';
                    $model_name = str_replace($model_folder.DIRECTORY_SEPARATOR, '',$model_relative_name);
    
                    $models[$model_name] = $model_folder.'/'.$model_name;
                }
            }
    
            if (isset($models[$name]))
            {
                $this->load->model($models[$name]);
                return $this->$name;
            }
            else //Try a library if we cannot load a model
            {
                $this->load->library($name);
                return $this->$name;
            }
    
            return NULL;
        }
    }
    
    
    
    <?php
    class MY_Controller extends CI_Controller 
    {
    
        //Lazy loading based on http://stackoverflow.com/questions/17579449/model-library-lazy-load-in-codeigniter
        public function __construct()
        {
            foreach (is_loaded() as $var => $class)
            {
                 $this->$var = '';
            }
    
            $this->load = '';
            parent::__construct();
        }
    
    
        // Lazy load models + libraries....If we can't load a model that we have; then we will try to load library $name
        public function __get($name)
        {
            //Cache models so we only scan model dir once
    
            static $models = FALSE;
            $this->load->helper('file');
    
            if (!$models)
            {
                $model_files = get_filenames(APPPATH.'models', TRUE);
                foreach($model_files as $model_file)
                {
                    $model_relative_name = str_replace('.php','',substr($model_file,strlen(APPPATH.'models'.DIRECTORY_SEPARATOR)));
                    $model_folder = strpos($model_relative_name, DIRECTORY_SEPARATOR) !== FALSE ? substr($model_relative_name,0,strrpos($model_relative_name,DIRECTORY_SEPARATOR)) : '';
                    $model_name = str_replace($model_folder.DIRECTORY_SEPARATOR, '',$model_relative_name);
    
                    $models[$model_name] = $model_folder.'/'.$model_name;
                }
            }
    
            if (isset($models[$name]))
            {
                $this->load->model($models[$name]);
                return $this->$name;
            }
            else //Try a library if we cannot load a model
            {
                $this->load->library($name);
                return $this->$name;
            }
    
            return NULL;
        }
    }