包含PHP文件的最佳实践是什么?


What is the best practice for including PHP files?

包含PHP文件的最佳实践是什么?

最好包含包含所有项目PHP文件的include.php文件吗?或者将其包含在需要它们的文件中

现在,我的项目在我的index.php文件中有几个包含文件。在index.php中包含所有php文件是否会降低效率?

最后,应该在哪里包含会话检查PHP文件?在所有PHP文件中?

EDIT 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视图位于控制器中。这对我很有用,我希望这对你也有帮助。

使用include.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 {}

现在您必须在需要该类的每个文件中使用'use'关键字。很酷的是,命名空间会自动添加到自动加载函数的类名中你可以这样做

function on_load($class)
{ require_once('root/' . str_replace('''', '/' $class)); }

如果你想了解更多,试试谷歌PHP自动加载,有很多关于这个主题的信息。但话又说回来。从你的问题的格式来看,我不认为你在使用OOP,所以这个答案只是给那些在谷歌上发现这个问题的人。


编辑

我还想补充以下内容:

 // 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
  • 使用include或require而不使用_once意味着每次执行语句时都会包含文件
  • 对模板或用户生成的文件使用include,对类使用require_once

考虑简单点,尽可能少地考虑负载,不要包含不必要的东西。

对于PHP文件。如果您在不同的页面上包含相同的php文件,只需创建一个php文件,其中包含这些文件。如果你只在1页或2页中使用PHP文件,请将它们单独包含。

我希望我对你有所帮助;)

独立包含文件,使用require_once()函数而不是include,因为require_once只允许包含一个文件…