douzong5057 2009-11-01 20:09
浏览 12
已采纳

基于控制器的类加载更好吗? (笨/ KohanaPHP /等。)

If you have worked with some of the frameworks like CodeIgniter or KohanaPHP, then you probably have seen that they are built so that the controller loads everything. Therefore, if you are in a library and wish to load additional resources you must get a copy of the controller instance and then use that to load the additional classes.

$this->c = get_instance();
$this->c->load->library('other_lib');

I'm wonding if it would be bad form to offload the class loading to another library so that you do not have to be tied to the controller instance. For example,

$this->other_lib = load::library('other_lib');
//vs
$this->load->library('other_lib');

Am I violating any MVC principals here? I know loading resources from models is bad - but what about other library packages that the controller isn't involved in?

  • 写回答

1条回答 默认 最新

  • doumu8217 2009-11-01 20:18
    关注

    If you're developing a library which needs to access additional resources, then add those resources as dependencies of your library. This means, when you need a class from your library, pass in any required resources when it is created via the constructor.

    For example:

    class MyLib {
        public function __construct(DB $db) {
            $this->db = $db;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料