PHP函数set_include_path的一点疑问

php中文网
发布: 2016-06-06 20:50:53
原创
1243人浏览过

set_include_path(get_include_path() . P_S . '.' . P_S . $siteConf['baseDir']);
set_include_path(get_include_path() . P_S . '.' . P_S . $siteConf['libDir']);
set_include_path(get_include_path() . P_S . '.' . P_S . $siteConf['baseDir'] . 'app');

代码如上,p_s即path_separator

立即学习PHP免费学习笔记(深入)”;

问题:
为什么每个路径前都要加'.'这个呢?

我查了下手册,在评论中有这么一个,这说明加它还是有必要的,但是原因还是未知....

joel at pittet dot ca17-Nov-2010 02:46

Seems set_include_path wasn't working for me.

The problem was i didn't have .: in my include_path
Which seemed to stop the set_include_path().

回复内容:

set_include_path(get_include_path() . P_S . '.' . P_S . $siteConf['baseDir']);
set_include_path(get_include_path() . P_S . '.' . P_S . $siteConf['libDir']);
set_include_path(get_include_path() . P_S . '.' . P_S . $siteConf['baseDir'] . 'app');

代码如上,p_s即path_separator

立即学习PHP免费学习笔记(深入)”;

问题:
为什么每个路径前都要加'.'这个呢?

我查了下手册,在评论中有这么一个,这说明加它还是有必要的,但是原因还是未知....

joel at pittet dot ca17-Nov-2010 02:46

Seems set_include_path wasn't working for me.

The problem was i didn't have .: in my include_path
Which seemed to stop the set_include_path().

"."表示当前目录,":"是两个不同的include path值的分隔符。

鼎峰企业智能建站系统0.1.5(开源版)
鼎峰企业智能建站系统0.1.5(开源版)

鼎峰企业智能建站系统是一个非常灵活的企业建站工具(简称:dfeiew),网页设计师可以使用dfeiew来快速建立企业网站。dfeiew采用adodo作为数据库持久层,采用smarty模板引擎,美工灵活,而且smarty是编译型的,访问快速。鼎峰拥有php+mysql,asp+access/ms sql版本,并且都是开源、免费的!快速提供企业建站传统的cms体系结构过于复杂,不适合做企业站点,而鼎峰

鼎峰企业智能建站系统0.1.5(开源版) 0
查看详情 鼎峰企业智能建站系统0.1.5(开源版)

include_path中不包含".",PHP碰到include($file), require($file)语句时就在当前目录(如果访问的是/var/www/html/my_app/index.php,当前目录就是/www/html/my_app/)寻找$file,直接去其它include_path中寻找。

include_path中加不加"."要看你会不会用相对路径包含当前目录下的文件,PHP引擎并不强求。

真机测试

测试表明,不管是改ini,还是在代码里调set_include_path(),都可以不带"."
图1:ini中设置include_path
PHP函数set_include_path的一点疑问

图2:ini中不设置include_path,代码中调用set_include_path()
PHP函数set_include_path的一点疑问

图3:ini中设置include_path,代码中调用set_include_path()
PHP函数set_include_path的一点疑问

图4:ini中设置include_path(包含当前目录),代码中调用set_include_path()
PHP函数set_include_path的一点疑问

此外,如果代码中不调用set_include_path(),ini中也不设置include_path,直接echo get_ include_path(),会输出"."

还有,第一次测试的时候,先set_include_path("/var"),再echo get_include_path(),居然什么都没输出,但我再也没能重现这个现象,不确定是不是当时test.php属于root用sudo的缘故(理论上讲不是,但就是没能重现)

延伸阅读

想了解include的更多详情,你可以看我前两周写的一篇文章《细说PHP类库自动加载》:http://weibo.com/1779910713/zg59ehZCd,第二页,第一段。

这货估计是直接 set_include_path("/some/path"); 于是在 include(当前目录下的文件) 的时候出错了。

这就类似于在linux下执行当前目录下的文件不用上 ./ 会出错一样。

如果你用了get_include_path() 去拼的话就不需要这样了。你可以把get_include_path()的值echo出来看看,里头已经包含了 "."

相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号