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?

图片转代码服务由CSDN问答提供 功能建议

在CodeIgniter中,我有一个名为 User_Model 的模型,另一个名为 Product_Model < / code>

现在在 User_Model 构造函数中,我正在加载我在类中使用的几个模型。

   class User_Model扩展CI_Model 
 {
公共函数__construct()
 {
 parent :: construct(); 
 $ this-&gt; load-model(“Product_Model”); 
 //加载 我需要的更多模型... 
} 
} 
   
 
 

在处理产品时,我有时需要使用用户模型,所以我这样做:

  class Product_Model扩展了CI_Model 
 {
公共函数__construct()
 {
 
internal :: construct(); 
 $ this-&gt; load-model  (“User_Model”); 
 //加载我需要的更多模型... 
} 
} 
   
 
 

问题在于,因为它们在 循环引用,我得到致命错误:达到'100'的最大函数嵌套级别。 我正在使用 xdebug ,我知道它有删除它的设置。 我的问题是 - 在不增加设置限制的情况下处理此问题的正确方法是什么。 我应该如何重构体系结构?

我知道如果我使用 PHP 而没有 codeigniter ,并创建循环引用(A类) 加载类B,类B加载类A),PHP将加载它N次(我认为它是3)然后将其标记为 * recursive *。 < 但是我们面对这样的情况,重构的最佳方法是什么?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题