PHP 中的 include 和 include_once
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6374399/
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
include and include_once in PHP
提问by Abishek
Could someone please explain to me on when should one use the include and include_once and how should one be using it.
有人可以向我解释什么时候应该使用 include 和 include_once 以及应该如何使用它。
I am new to PHP and would like to understand this is laymans terms. Not too clear with whats mentioned on PHP documentation at www.php.net.
我是 PHP 新手,想了解这是外行术语。不太清楚 www.php.net 上的 PHP 文档中提到的内容。
Let us say I have the following folder structure
假设我有以下文件夹结构
-->Root-->API-->User.php
-->Root-->Users-->CreateUser.php
If I have to use the User.php in CreateUser.php how should I be doing it.
如果我必须在 CreateUser.php 中使用 User.php 我应该怎么做。
And if there is another file under
如果下面还有另一个文件
-->Root-->API-->Utility-->ImageProcessor.php
How should i being including ImageProcessor.php with CreateUser.php
我应该如何在 CreateUser.php 中包含 ImageProcessor.php
回答by AhmetB - Google
If you always include_once
everything will be ok. It prevents including the same file twice.
如果你总是include_once
一切都会好的。它可以防止两次包含同一个文件。
Let's say you have these files:
假设您有这些文件:
c
: include b; include a;
c
: include b; include a;
b
: include a;
b
: include a;
a
: echo "hello";
a
: echo "hello";
when you execute c
, a
will be included twice (most probably an unwanted situation), therefore if you use all of them as include_once
it will be called only once.
当您执行c
,a
将被包含两次(很可能是不需要的情况),因此如果您使用所有它们,因为include_once
它只会被调用一次。
It comes with a little performance cost however if you are not Facebook or so, that is not a significant cost.
它带来了一点性能成本,但是如果你不是 Facebook 左右,那不是一个很大的成本。
回答by afaf12
I'll use simple examples, one for situation when include() is better, one when include_once() is better.
我将使用简单的示例,一个用于 include() 更好的情况,一个用于 include_once() 更好的情况。
Let's say you have files a.php and b.php . Each of them "includes" library.php which contains function foo(). Now, if you try to "include" both a.php and b.php in another file, index.php, for example, you could get an error saying that foo() was already declared. Which means include_once() is better suited in this situation. You'll avoid defining the same function twice.
假设您有文件 a.php 和 b.php 。他们每个人都“包括”包含函数 foo() 的 library.php。现在,如果您尝试将 a.php 和 b.php 同时“包含”在另一个文件 index.php 中,例如,您可能会收到一个错误,指出 foo() 已经被声明。这意味着 include_once() 更适合这种情况。您将避免两次定义相同的函数。
Second case. Let's assume you want to "include" file.php every time your loop runs. Content of file.php could be just simple html output.
第二种情况。假设您想在每次循环运行时“包含”file.php。file.php 的内容可能只是简单的 html 输出。
file.php:
文件.php:
<?php echo "User No: " . $i; ?>
index.php:
索引.php:
<?
for($i=1; $i<=10; $i++){
include("file.php");
}
In this case, include() is better because it will include the file every time the loop runs. Include_once() would only work the first time.
在这种情况下,include() 更好,因为每次循环运行时它都会包含文件。Include_once() 只会在第一次工作。
回答by AllisonC
In CreateUser.php:
在 CreateUser.php 中:
include_once("/API/User.php");
include_once("/API/Utility/ImageProcessor.php");
If it is necessary to make sure these files are included, you should use require_once instead...
如果有必要确保包含这些文件,则应改用 require_once...
回答by LeleDumbo
Use include_once when there's a implicitly nested requirement (any better term?) involved. I mean something like this:
当涉及隐式嵌套需求(任何更好的术语?)时使用 include_once 。我的意思是这样的:
Assume
认为
- A is a department model
- B is a employee model
- C is a database module
- A是部门模型
- B是员工模型
- C是一个数据库模块
Because model needs database connection, A and B both includes C. A department consists of employees working there, so A includes B. With include_once, the database module would only be included just once, so there won't be any duplicate declaration error.
因为model需要连接数据库,A和B都包含C。A部门由员工组成,所以A包含B。使用include_once,数据库模块只会被包含一次,所以不会出现重复声明错误。
Include is for something more general like a template of output (perhaps echoing active username) that is intentionally designed to appear multiple times.
Include 用于更一般的东西,比如输出模板(可能是响应活动用户名),它有意设计为多次出现。
回答by Bhanu Prakash Ryaga
The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once. Lets say for example, i have three files,
include_once() 语句在脚本执行期间包括并评估指定的文件。这是一种类似于 include() 语句的行为,唯一的区别是如果来自文件的代码已经被包含,则不会再次被包含。顾名思义,它将只包含一次。比方说,我有三个文件,
FILES:
文件:
- FUNCTIONS.PHP
- GLOBALS.PHP
- HEADER.PHP
- 函数.PHP
- 全局变量
- 头文件
This is how each file looks like:
这是每个文件的样子:
FUNCTIONS.PHP
函数.PHP
<?php
function foo(){
echo 'some code';
}
?>
GLOBALS.PHP:
全球.PHP:
<?php
include('FUNCTIONS.PHP');
foo();
?>
HEADER.PHP
头文件
<?php
include('FUNCTIONS.PHP');
include('GLOBALS.PHP');
foo();
?>
Now if you try to open HEADER.PHP you will get an error because GLOBALS.PHP includes FUNCTIONS.PHP already. You will get an error saying that function foo() was already declared in GLOBALS.PHP, and I also included in HEADER.PHP - which means I have included FUNCTIONS.PHP two times. So to be sure I only include FUNCTIONS.PHP only ONE time, I should use the include_once() function, so my HEADER.PHP should look like this: HEADER.PHP
现在,如果您尝试打开 HEADER.PHP,您将收到错误消息,因为 GLOBALS.PHP 已经包含 FUNCTIONS.PHP。您将收到一条错误消息,指出函数 foo() 已在 GLOBALS.PHP 中声明,并且我也包含在 HEADER.PHP 中 - 这意味着我已包含 FUNCTIONS.PHP 两次。所以为了确保我只包含 FUNCTIONS.PHP 一次,我应该使用 include_once() 函数,所以我的 HEADER.PHP 应该是这样的: HEADER.PHP
<?php
include_once('FUNCTIONS.PHP');
include('GLOBALS.PHP');
?>
Now when I open HEADER.PHP, I will not get an error anymore because PHP knows to include the file FUNCTIONS.PHP only ONCE So to avoid getting an error, it would just be safe to use include_once() instead of include() function in your PHP code.
现在,当我打开 HEADER.PHP 时,我不会再出现错误,因为 PHP 知道只包含文件 FUNCTIONS.PHP 一次所以为了避免出现错误,使用 include_once() 而不是 include() 函数是安全的在您的 PHP 代码中。