【发布时间】:2012-01-16 19:15:44
【问题描述】:
假设我有以下文件
示例 1
functions.php
<?php
session_start();
function getSessionData(){
...
// returns array
}
?>
index.php
<?php
session_start();
include("functions.php");
$sessionData = getSessionData();
?>
<html>
<head>
<title>test</title>
</head>
<body>
<?php print_r($sessionData); ?>
</body>
</html>
现在index.php 包含functions.php。因为我在index.php中有session_start(),这是否意味着它会自动添加到functions.php中(看到函数包含在索引中?)
我不确定我是否说清楚了。
示例 2
config.php
<?php
$url = "www.example.com";
?>
functions.php
<?php
include("config.php");
function getSomething(){
...
return $url
}
?>
index.php
<?php
include("config.php");
include("functions.php");
$some_var = getSomething();
?>
<html>
<head>
<title>test</title>
</head>
<body>
<?=$some_var;?>
</body>
</html>
现在 functions.php 和 index.php 都包含 config.php...
但是因为 config 已经包含在 index.php...
这是否意味着它必须包含在函数中?
我想我只是把自己弄糊涂了:)
【问题讨论】:
-
您可以根据需要多次包含一个文件,只要它不定义任何函数/对象。如果您双重包含函数定义文件,您将收到“无法重新声明任何()”致命错误。简单的变量、输出块等等……都可以。