如何最佳地包含PHP文件?
是将所有项目PHP文件包含在include.php
文件中,还是在需要它们的文件中包含它们?
目前,我的项目在index.php
文件中有几个包含文件。在index.php中包含所有php文件会使它变得不那么高效吗?
最后,应该在哪里包含会话检查PHP文件?在所有PHP文件中都要包含吗?
如何最佳地包含PHP文件?
是将所有项目PHP文件包含在include.php
文件中,还是在需要它们的文件中包含它们?
目前,我的项目在index.php
文件中有几个包含文件。在index.php中包含所有php文件会使它变得不那么高效吗?
最后,应该在哪里包含会话检查PHP文件?在所有PHP文件中都要包含吗?
编辑 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 root . '/view/shared/header.php';?>
<div class="bla bla bla">bla bla bla</div>
<?include root . '/view/shared/footer.php';?>
我总是使用include
而不是include_once
,因为后者需要额外的检查。我的意思是,由于我非常确定我只包含每个文件一次,所以我不需要使用include_once
。这样,您也可以知道哪个包含在哪里。例如,您知道关键文件(如db.php或functions.php)位于config.php中。或者您知道包含视图位于控制器中。对我来说非常有用,希望对您也有所帮助。
include.php
文件是我认为非常好的实践,因为它在更改大型项目中包含的文件方面非常有帮助。如果项目很小,那么包含单个文件不是问题。但随着项目的扩大,管理这些文件就成了问题。保持简单,尽量减轻负担,不要包含不必要的东西。
所以对于你的 PHP 文件。如果在不同的页面上包含同样的 PHP 文件,只需创建一个包含这些文件的 PHP 文件。 如果您仅在1或2个页面中使用PHP文件,请将它们单独包含。
我希望我能帮到你;)
我不确定你是否使用面向对象编程,但如果你使用的话,这里可能有一个好的答案。
在PHP中,你可以定义一个名为自动加载器的函数,如果你尝试创建一个未定义的类的对象,则会调用自动加载器。然后,你可以使用类名来找出包含该类的文件存储在哪里,以便在最后一刻将其包含进来。以下是一个示例...
<?php
function on_load($class)
{
if(file_exists(require_once('classes/'.$class.'.php')))
{
require_once('classes/'.$class.'.php');
}
else
{
throw new Exception('Class not found: '.$class.' in classes/');
}
}
spl_autoload_register('on_load'); // tell php to call your on_load function if the class was not defined
/classes/database/MySQL.php
/classes/database/PDO.php // I'm just listing random stuff
/classes/Core.php // Whatever
/classes/datastructure/HashMap.php
然后,您可以使用特殊的命名约定来找到正确的目录
class Database_MySQL{} // look in <root_dir>/database/ for MySQL.php
class Core // look in <root_dir>/ for Core.php
class Database_Driver_Adapter_Useless_MysqlAdapterThingy {} // look in <root_dir>/Database/Driver/... blabla
或者您可以使用 PHP 5.3 的方式来定义您的类,如下所示:
<?php
namespace database\driver\adapter\useless;
use database\driver\adapter\MysqlAdapter; // Now you have to tell PHP which version of MysqlAdapter class you want to use, even if there is just one
class MysqlAdapterThingy extends MysqlAdapter {}
function on_load($class)
{ require_once('root/' . str_replace('\\', '/' $class)); }
如果你想了解更多,可以尝试搜索 PHP 自动加载,这个主题有大量的信息。但是,从你的问题格式来看,我不认为你在使用面向对象编程,所以这个答案仅适用于那些通过谷歌搜索到这个问题的人。
我还想添加以下内容:
// Only include the file once even if you place this statement multiple times
require_once('bla.php');
include_once('bla.php');
require('bla.php'); // Error if file doesn't exist, php will not continue
inlcude('bla.php'); // Warning if file doesn't exist, but php will continue
if(file_exists(require_once('classes/'.$class.'.php')))
应该改为 if(file_exists('classes/'.$class.'.php'))
。 - user559533独立包含文件时,请使用require_once()
函数而不是include
,因为require_once
只允许文件被包含一次...