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条)

报告相同问题?