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 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致