a simple & optimized way to include once
function my_include_once($path) {
if ( !defined($path) ) {
include($path);
defined($path, 1);
}
}
// sample
my_include_once("/home/www/class/object.php");
include_once()The include_once() 语句在脚本执行期间包含并运行指定文件。此行为和 include() 语句类似,唯一区别是如果该文件中的代码已经被包含了,则不会再次包含。如同此语句名字暗示的那样,只会包含一次。 include_once() 应该用于在脚本执行期间同一个文件有可能被包含超过一次的情况下,想确保它只被包含一次以避免函数重定义,变量重新赋值等问题。 使用 require_once() 和 include_once() 的更多例子见最新的 PHP 源程序发行包中的 PEAR 代码。 返回值和 include() 相同。如果文件已被包含,本函数返回 TRUE。
参见 include(),require(),require_once(),get_required_files(),get_included_files(),readfile() 和 virtual()。
add a note
User Contributed Notes
tszming at gmail dot com
17-Oct-2006 10:35
a simple & optimized way to include once
webmaster AT domaene - kempten DOT de
10-Aug-2006 08:11
Since I like to reuse a lot of code it came handy to me to begin some sort of library that I stored in a subdir
29-Aug-2005 04:52
Dealing with function redefinitions
flobee at gmail dot com
26-May-2005 10:55
i already had a discussion with several people about "not shown errors"
Pure-PHP
18-Mar-2005 06:17
Inlude_once can slower your app, if you include to many files.
bioster at peri dot csclub dot uwaterloo dot ca
29-Oct-2004 06:06
Something to be wary of: When you use include_once and the data that you include falls out of scope, if you use include_once again later it will not include despite the fact that what you included is no longer available. | ||||||