技术上include()
意思好像你在你的PHP点插入包含脚本的文本采取行动。因此:
includeMe.php:
<?php
$test = "Hello, World!";
?>
includeIt.php:
<?php
include('includeMe.php');
echo $test;
?>
应该是完全一样的:
<?php
/* INSERTED FROM includeMe.php */
$test = "Hello, World!";
/* END INSERTED PORTION */
echo $test;
?>
认识到这一点,使得功能动态,包括文件的想法使大约多大意义(与大约是容易做到)因为它们具有动态代码。这是可能的,但它会涉及到很多元变量。
我想看看PHP中的Variable Variables以及将变量带入全局范围的get_defined_vars函数。这可能会喜欢的东西来完成:
<?php
define('E_ROOT', str_replace('//','/',dirname(__FILE__)));
/* ... */
function e_load($fn, $allowReloading = FALSE) {
$prev_defined_vars = get_defined_vars();
$inc = E_ROOT.'/path/here/'.$fn.'.php';
if($allowReloading)
require $inc; // !!!
else
require_once $inc; // !!!
$now_defined_vars = get_defined_vars();
$new_vars = array_diff($now_defined_vars, $prev_defined_vars);
for($i = 0; $i < count($new_vars); $i++){
// Pull new variables into the global scope
global $$newvars[$i];
}
}
?>
它可能会更方便,只需使用require()
和require_once()
代替e_load()
注意,函数和常量应该始终是在全球范围内,所以不管他们被定义在哪里,他们都应该可以在代码中的任何地方被调用。
对此的一个例外是在类中定义的函数。这些只能在类的名字空间内调用。
编辑:
我只是自己测试了一下。函数在全局范围内声明。我跑到下面的代码:
<?php
function test(){
function test2(){
echo "Test2 was called!";
}
}
//test2(); <-- failed
test();
test2(); // <-- succeeded this time
?>
所以test()
已经运行后的功能只是定义,但功能是再从外面test()
调用。因此,通过我之前提供的脚本,您应该需要将全局范围拉到您的变量中。
如果它真的是*库*,而不是模块,那么它包含在 – 2010-10-03 15:00:18
中的范围并不重要,这是什么意思? – 2010-10-03 15:03:15
我的意思是图书馆通常只代表一个包含函数/类的文件 – 2010-10-03 16:05:31