编辑2016
好吧,我回答这个问题已有5年了.我还活着.很多都改变了.
基本上,我们的想法是拥有一个合适的文件夹结构(例如PSR-4标准)并在每个文件中都有一个类.这样您就可以使用自动加载器,PHP将自动加载您的文件.
老答复
通常,我有一个这样的配置文件:
define(root, $_SERVER['DOCUMENT_ROOT']);
.... // other variables that are used a lot
include (root . '/class/database.php');
.... // other includes that are mostly called from each file, like a db class, or user class, functions etc etc...
if (defined('development'))
{
// turn error reporting on
}
else
{
// turn it off
}
etc etc... You got the point of config.
我在每个文件中包含config.php.我忘了现在该怎么做,但是apache可以为你做自动包含.因此,默认情况下,您可以说apache包含您的配置文件.
然后,我有控制器类,它调用视图.在每个函数中我都调用了视图.
someController.php
function index() { include root . '/views/view_index.php'; }
最后,从视图来看,如果我需要包含页眉和页脚视图,我会这样做:
view_index.php
我总是在这个结构中使用include而不是include_once,因为后者需要额外的检查.我的意思是,因为我很确定我只包含一次文件,所以我不需要使用include_once.这样,你也知道哪个包含在哪里.例如,您知道像db.php或functions.php这样的关键文件位于config.php中.或者您知道包含视图位于控制器中.这对我来说非常有用,我希望这对你也有帮助.