您的位置:首页 > 编程语言 > PHP开发

php手记-set_include_path和get_include_path用法详解

2015-11-27 15:06 846 查看

set_include_path

先看效果

//假如有段代码是这样的:
include("123/test1.php");
include("123/test2.php");
include("456/test3.php");
require("123/test4.php");
require("123/test5.php");

//现在要把123的目录换了换成567,这下是不是要搬砖了,要改的地方很多,如果在其他php文件里也引用了123目录下的php那就更麻烦了

//再看下面的代码 是不是感觉好很多
set_include_path("123");
include("test1.php");
include("test2.php");
include("test3.php");
require("test4.php");
require("test5.php");
//在进一步 设置一下 456
set_include_path("123:456");//路径间用:隔开,也可以用PATH_SEPARATOR代替:,如set_include_path("123".PATH_SEPARATOR."456")
include("test1.php");
include("test2.php");
include("test3.php");
require("test4.php");
require("test5.php");
//如果123、456目录下都有test1.php,那么那么那个路径在前就使用其下的test1.php,那么上例中使用的肯定是123/test1.php了


作用:

简化文件的管理

get_include_path

作用

获取当前的 include_path 配置选项

例子

set_include_path("test2:test1:test3");
echo get_include_path();
//output:test2:test1:test3


因此在set_include_path时需要用get_include_path获得之前设定的path(可以是用set_include_path设定 或者在php.ini里面设定的)拼到要设定的路径的前面,避免把被人或系统设定的path替换掉如:

//别人设定的  other.php
set_include_path("test2:test1:test3");
echo get_include_path();//output:test2:test1:test3
// my.php
set_include_path(get_include_path().PATH_SEPARATOR."test4:test5:test6");
echo get_include_path();//output:test2:test1:test3:test4:test5:test6
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php