douzhang3356 2010-11-17 14:30
浏览 45
已采纳

PHP:在我自己的MVC框架中包含文件?

I've just started to build my very own MVC framework. Feel's kind of nice to know everything from the ground up and only get the stuff that's really necessary for my app.

I come from a codeIgniter background which helped me to get into the MVC perspective of seeing things. In codeigniter, to include a file, codeIgniters very own load class is used.

This load class, when loading a file, checks if a file have previously been included, and if not includes it, which ensures that a file isn't included twice.

However this method of including files has the downside of making it impossible (?) to run tests on my files or take advantage of PHPdoc in my IDE, which I need.

Clearly, I can use the normal include & require functions in PHP forwards and backwards across my application, where a certain library would be needed, but it clearly won´t be good to possible include the same file twice...

So - what's a good solution to use in my own PHP5 MVC framework to include files?

  • 写回答

4条回答 默认 最新

  • dpwbc42604 2010-11-17 14:34
    关注

    I'm doing a similar thing to you, and I'm using autoload.

    Just put this in index.php:

    function __autoload($class_name) {
        include $class_name . '.php';
    }
    

    or include whatever logic you need to check multiple directories.

    Edit: Here's my (slightly flaky) code for checking an alternate path. It could be done a lot more cleanly if you were going to need to check multiple paths.

    function __autoload($class_name) {
      $path = INC_PATH . strtolower($class_name) . '.php';
    
      if (!file_exists($path)) {
        $path = MODELS_PATH . strtolower($class_name) . '.php';
      }
    
      require $path;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器