包括多个文件或仅包含一个文件


Including many files or just one file

设置项目结构;我在想最好有一个"bootstrap.php"文件,其中包含和设置数据库连接、会话管理、库、错误处理等所有内容。或者只是单数包含当前所需的文件?(这需要先于包含的顺序,等等)。

备选方案1:

索引.php

<?php
include 'bootstrap.php';
//session... database up and ready to be used...

备选方案2:

索引.php

    <?php
include 'session.php';
include 'database.php';
include 'errorhandling.php';
//now use database...

仅包含特定页面所需的文件。例如,如果页面不需要数据库连接,则不会包含它。因此,后一种方法最好有选择地包含所需的文件,除非您在每个页面上都导入一些常见内容。

此外,PHP-Autoload 对你来说可能很有趣: http://php.net/manual/de/language.oop5.autoload.php