惰性加载模型和库中的CI 3 __get魔法方法的控制器 [英] lazy load models and libraries in CI 3 __get magic method of controller

查看:134
本文介绍了惰性加载模型和库中的CI 3 __get魔法方法的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为CI 3.0.4中的模型+库写一个惰性加载器

I am attempting to write a lazy loader for models + libraries in CI 3.0.4

我试图得到如下所述的

CodeIgniter中的模型/库延迟加载< a>

Model/library lazy load in CodeIgniter

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

This code seems to be for CI version 2 and when I tried to apply it to CI version 3.0.4 I get the following errors.

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

这是我做的:


  1. 创建文件应用程序/ core / MY_Controller.php

  1. Create file application/core/MY_Controller.php

创建以下代码:



    //Lazy load based on http://stackoverflow.com/questions/17579449/model-library-lazy-load-in-codeigniter
    function __get($name)
    {
       if (isset($this->$name) && $this->$name instanceof CI_Model) 
        {
              return $this->$name;
        }
    }
  }



b $ b

一旦我可以让这个工作没有错误;我将添加我的自定义逻辑来加载正确的模型或库。

Once I can get this to work without errors; I will add my custom logic to load the right model or library.

我想这样做而不修改核心。

I want to do this WITHOUT modifying core.

推荐答案

基于例子,我想出了以下不需要核心修改和工作在CI 3.0.4。

Based on example I have come up with the following that does NOT require core modifications and works in CI 3.0.4. It first tries to load a model and if the model does NOT exist it attempts to load a library.


  1. 创建文件应用程序/ core / MY_Controller.php

  1. Create file 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;
    }
}


这篇关于惰性加载模型和库中的CI 3 __get魔法方法的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆