set_include_path和get_include_path用法详解
2015-12-08 17:54
661 查看
转自:http://www.jianshu.com/p/303feaaeded1
这样会引入很多外部文件,但如设置set_include_path('123/')可用下面代码代替。
因为,执行include或require时,会去include_path指定路径查找要引入文件,虽然不知道性能是否优化,但可以节省代码。当然,还有玄机!
这个函数不仅可定义一个文件夹,还可定义很多文件夹。如下,写一个初始化函数:
它的路径成了:
前面的.;C:\php5\pear;是php.ini中include_path的默认值。按照这个思路,如果加载许多文件夹的话,就可以直接写文件名了。但是如果我就写一个
然后去引入其他文件夹的文件,就会报错,说在我指定的这个文件夹内找不到。
首先,先用另外一种方法输出一下:
结果是:D:\AppServ\www。我如果去www下找test4.php这个文件,则没有报错
但是我如果去找
就会报错:
而且我们还发现.;C:\php5\pear;已经被替换掉了。所以我们在使用的时候,如果不是仅在一个文件夹下引入文件,我们就需要在前面加上
需要注意两点,如在指定目录下找不到要求包含的文件,在当前页面目录下正好存在这个名称的文件时,则默认引入当前目录下的该文件。当指定了多个目录为 include_path ,而所要求包含的文件在这几个目录都有相同名称的文件存在时,php选择使用设定 include_path 时排位居前的目录下的文件。
推荐拓展阅读
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
¥ 打赏支持
set_include_path函数在脚本里动态地对PHP.ini中
include_path修改。include_path可以针对include和require的路径范围进行限定(预定义)。如果没设置这个值,则需要写完全的路径,例如:
include('123/test1.php'); include('123/test2.php');
这样会引入很多外部文件,但如设置set_include_path('123/')可用下面代码代替。
set_include_path('123/'); include('test1.php'); include('test2.php');
因为,执行include或require时,会去include_path指定路径查找要引入文件,虽然不知道性能是否优化,但可以节省代码。当然,还有玄机!
这个函数不仅可定义一个文件夹,还可定义很多文件夹。如下,写一个初始化函数:
function initialize() { set_include_path(get_include_path().PATH_SEPARATOR . 'core/'); set_include_path(get_include_path().PATH_SEPARATOR . 'app/'); set_include_path(get_include_path().PATH_SEPARATOR . 'admin/'); set_include_path(get_include_path().PATH_SEPARATOR . 'lib/'); set_include_path(get_include_path().PATH_SEPARATOR . 'include/'); set_include_path(get_include_path().PATH_SEPARATOR.'data/'); set_include_path(get_include_path().PATH_SEPARATOR.'cache/'); }
它的路径成了:
.;C:\php5\pear;core/;app/;admin/;lib/;include/;data/;cache/
前面的.;C:\php5\pear;是php.ini中include_path的默认值。按照这个思路,如果加载许多文件夹的话,就可以直接写文件名了。但是如果我就写一个
set_include_path(dirname(__FILE__));
然后去引入其他文件夹的文件,就会报错,说在我指定的这个文件夹内找不到。
首先,先用另外一种方法输出一下:
<?php set_include_path(dirname(__FILE__)); $include_value = ini_get('include_path');//这里的ini_get()函数注意 echo $include_value; ?>
结果是:D:\AppServ\www。我如果去www下找test4.php这个文件,则没有报错
include('test4.php');
但是我如果去找
include('test1.php');
就会报错:
Warning: include() [function.include]: Failed opening 'test1.php' for inclusion (include_path='D:\AppServ\www') in D:\AppServ\www\test.php on line 6
而且我们还发现.;C:\php5\pear;已经被替换掉了。所以我们在使用的时候,如果不是仅在一个文件夹下引入文件,我们就需要在前面加上
get_include_path().PATH_SEPARATOR .。
get_include_path()是获取当前
include_path的默认值。
PATH_SEPARATOR是个常量,是include的路径分界符合,window上是;在unix和Linux上是。最后,也可以通过另外一种原始的方法整合文件。
ini_set('include_path', '目录名');
需要注意两点,如在指定目录下找不到要求包含的文件,在当前页面目录下正好存在这个名称的文件时,则默认引入当前目录下的该文件。当指定了多个目录为 include_path ,而所要求包含的文件在这几个目录都有相同名称的文件存在时,php选择使用设定 include_path 时排位居前的目录下的文件。
推荐拓展阅读
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
¥ 打赏支持
相关文章推荐
- 一Flash从入门开发者放弃了成长之路
- 信息安全系统设计基础实验三
- _________________________________________________杭电题目分类________________________________________________________
- Base64 js实现demo,支持中文
- OptionMenu 和 ContextMenu (系统菜单栏——上下文菜单栏)
- ZZULIOJ 1798 戴姆勒的求助
- C# 关键字extern用法
- 信息安全系统设计基础实验二
- 1-3-08:温度表达转化
- 学习 easyui 之二:jQuery 的 ready 函数和 easyloader 的加载回调函数
- php报错: PHP Warning: PHP Startup: memcache: Unable to initialize module
- Apktool 打包签名后安装出现 INSTALL_PARSE_FAILED_NO_CERTIFICATES错误
- 触发哭
- POJ 3080 Blue Jeans
- ShareSDK自定义分享的面板
- Android Studio第四期 - 导入第三方类库的方法
- BZOJ 1500: [NOI2005]维修数列
- 信息安全系统设计基础实验一
- NYOJ 10 skiing(好题)
- discuz云平台报调用远程接口失败的问题分析和解决