开发者

PHP: Including files in my own MVC framework?

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 incl开发者_开发问答udes 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?


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;
}


just use include_once and require_once . while CI has a load class, you're by no means required to use it.


use include_once/ require_once but the best solution would be working with a autoloader

http://php.net/manual/de/language.oop5.autoload.php

what IDE do you use?


Generally php has the built in include_once which as the name says includes the file, but only once.

Or you can use autoloading which most IDEs support (though some need a bit of a hint - depends on what IDE are you using).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜