duanpo7354 2014-07-03 12:13
浏览 79
已采纳

在php / codeigniter中加载循环引用类

In CodeIgniter I have a model named User_Model and another one named Product_Model

Now in the User_Model constructor I am loading couple of models that I use in the class.

class User_Model extends CI_Model
{
  public function __construct()
  {
    parent::construct();
    $this->load-model("Product_Model");
    //load some more models I need...
  } 
}

While working on products, I need sometimes to be using the user model, so I do:

class Product_Model extends CI_Model
{
  public function __construct()
  {
    parent::construct();
    $this->load-model("User_Model");
    //load some more models I need...
  } 
}

The problem is that since these are in circular reference, I am getting Fatal error: Maximum function nesting level of '100' reached. I am using xdebug and I know that it has settings to remove this. My question is - what is the correct way to handle this without increasing setting limitations. How should I restructure the architecture?

I know that if I am using PHP without codeigniter, and create circular references (class A loads class B, class B loads class A), PHP will load it for N times (I think it is three) and then mark it as *recursive*.

But we are faced with such a situation, what is the best way to refactor?

  • 写回答

1条回答 默认 最新

  • dp20011 2014-07-04 04:59
    关注

    CI 2.2.0 does not allow autoloading model with alias but 3.0-dev does support it. since you have CI 2.2.0 you need to copy a function from 3.0-dev core to your 2.2.0 core.

    Seps :

    1. Get CI 3.0-dev from Github
    2. Open system/core/loader.php of CI 3.0-dev.
    3. Find public function model function.
    4. Replace it in your system/core/loader.php of CI 2.2.0.
    5. Now you can autoload like $autoload['model'] = array('first_model' => 'first');

    Note : This is solution for those who want autoloading model with alias but having CI 2.2.0

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题