如何有效地包含 config.php?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1712973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include config.php efficiently?
提问by Strawberry
I'm always interested in more efficient ways and learning new things. Right now, I am using the code <?php include('config.php'); ?>in each and every file. Depending on where the file is in my the folder structure, I would have <?php include('../config.php'); ?>or even <?php include('../../config.php'); ?>. How can I make it more efficient? Is there a way to just have it my config.php in root and make everything in root require config.php?
我总是对更有效的方法和学习新事物感兴趣。现在,我正在使用<?php include('config.php'); ?>每个文件中的代码。根据文件在我的文件夹结构中的位置,我会拥有<?php include('../config.php'); ?>甚至 <?php include('../../config.php'); ?>. 我怎样才能让它更有效率?有没有办法让它在root中使用我的config.php并使root中的所有内容都需要config.php?
回答by user187291
there is a way to include a file automatically (auto_prepend_fileini setting), however the biggest improvement you can make is to abandon the usage of multiple php files and use index.php as a single entry point for the whole website.
有一种方法可以自动包含文件(auto_prepend_fileini 设置),但是您可以做出的最大改进是放弃使用多个 php 文件并使用 index.php 作为整个网站的单个入口点。
suppose you write a SO clone ;) with the pages "questions", "tags", "users", etc. On each page you need some generic php stuff (db, session) + common html elements (header, footer). A popular approach is to have a bunch of php files (questions.php, tags.php, users.php) each of them includes the common stuff. For example, users.php will look like this then
假设你用页面“问题”、“标签”、“用户”等编写了一个 SO 克隆 ;)。在每个页面上,你需要一些通用的 php 内容(db、session)+ 常见的 html 元素(页眉、页脚)。一种流行的方法是拥有一堆 php 文件(questions.php、tags.php、users.php),每个文件都包含常见的内容。例如,users.php 将如下所示
include 'db.php';
include 'session.php';
include 'html.header.php';
.....users-specific stuff
include 'html.footer.php';
This is quite tedious (you have to repeat lots of code) and inflexible (think adding a sidebar to all pages on the site). My suggestion is to make includes "inside out" that is, have a "common stuff" file that includes page-specific code:
这非常乏味(您必须重复大量代码)且不灵活(想想为网站上的所有页面添加侧边栏)。我的建议是使包含“由内而外”,即有一个包含页面特定代码的“通用内容”文件:
# index.php
db functions
session functions
html header
$page = isset($_GET['page'])
? preg_replace("/\W+/", "", $_GET['page'])
: "home";
include "$page.php";
html footer
Thus you'll have a single entry point on the website - this is more flexible and better for debugging. The only drawback is that urls are less "nice" (user.php vs index.php?page=user), but this can be easily solved with mod_rewrite
因此,您将在网站上有一个单一的入口点 - 这更灵活,更适合调试。唯一的缺点是网址不太“好”(user.php vs index.php?page=user),但这可以通过 mod_rewrite 轻松解决
回答by Asaph
Put the path containing config.phpin your php include pathand then you can simply do this:
把包含config.php在你的php 包含路径中的路径,然后你可以简单地这样做:
include 'config.php';
or better yet:
或者更好:
require_once 'config.php';
requireis preferred over includebecause it triggers an error instead of a warning when a file cannot be included for some reason (e.g. file not found, permissions error, etc.). The _oncesuffix is a good addition to make sure the same file isn't needlessly included multiple times.
require首选,include因为当由于某种原因(例如找不到文件、权限错误等)无法包含文件时,它会触发错误而不是警告。该_once后缀是一个很好的补充,以确保相同的文件不包含不必要多次。
Also, please note that you don't need the parenthesis around 'config.php'in your includecall. includeis not a function in php. It's a language construct. The parenthesis just serve as a needless grouping not unlike this example: $myVar = (2 + 2);.
另外,请注意,您'config.php'的include调用中不需要括号。include不是 php 中的函数。这是一种语言结构。括号只是作为一个不必要的分组,与这个例子不同:$myVar = (2 + 2);.
回答by alex
You can make a base dir constant
您可以使基本目录保持不变
define('BASE_DIR', realpath(__FILE__));
Put that in your index.php
把它放在你的 index.php 中
Then you can do
然后你可以做
include BASE_DIR . 'config.php';
回答by Kzqai
Different options that can be combined:
可以组合的不同选项:
Make path definitions in a resources file that won't be tracked in your source control. e.g. define('LIB_PATH', '/home/tchalvak/project/');
Specify an autoprepend file and set that up in the php.ini. e.g. including a global, used-everywhere list of scripts, which you can add to or subtract from on the fly.
Finally, a little bit of path-string-manipulation trickery that I found or came up with, I forget which:
require_once(substr(__FILE__, 0, (strpos(__FILE__, 'lib/')))."resources.php");// require a file in a specific relationship to another known file, regardless of the actual path involved, and without dealing with any defines.
在源代码管理中不会跟踪的资源文件中创建路径定义。例如define('LIB_PATH', '/home/tchalvak/project/');
指定一个 autoprepend 文件并在 php.ini 中进行设置。例如,包括一个全局的、无处不在的脚本列表,您可以在运行中添加或从中减去。
最后,我发现或想出了一些路径字符串操作技巧,我忘记了:
require_once(substr(__FILE__, 0, (strpos(__FILE__, 'lib/')))."resources.php");// 需要一个与另一个已知文件具有特定关系的文件,无论涉及的实际路径如何,并且不处理任何定义。
回答by cquezel
Here is how I go about managing my required (include) files :
以下是我管理所需(包含)文件的方法:
When I can, I never use constants or variables (to define the absolute path) in my required files. I find this has some advantages:
如果可以,我从不在所需文件中使用常量或变量(用于定义绝对路径)。我发现这有一些优点:
- I don't have to change a constant for every version of the website I deploy.
- Static analysis tools like PHPLint don't work with these types of inclusions.
- My editor (Eclipse) allows me to navigate to a required file by control + clicking on the required filename.
- It is easier to share files between projects (without having to edit the required paths)
- …
- 我不必为我部署的网站的每个版本更改常量。
- 像 PHPLint 这样的静态分析工具不适用于这些类型的包含。
- 我的编辑器 (Eclipse) 允许我通过控制 + 单击所需的文件名来导航到所需的文件。
- 在项目之间共享文件更容易(无需编辑所需的路径)
- …
But … to do this, all requires and includes need to be relative. By default PHP uses the current directory of the called script to establish it's include path. This can cause problems when the called scripts do not reside in the same folder. So the suggestion of having all requests passing through a single script automatically removes any such problems.
但是……要做到这一点,所有要求和包含都必须是相对的。默认情况下,PHP 使用被调用脚本的当前目录来建立它的包含路径。当调用的脚本不在同一文件夹中时,这可能会导致问题。因此,让所有请求通过单个脚本自动消除任何此类问题的建议。

