php手记-set_include_path和get_include_path用法详解

来源:互联网 发布:java多个线程并发实现 编辑:程序博客网 时间:2024/05/21 09:10

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");//在进一步 设置一下 456set_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.phpset_include_path("test2:test1:test3");echo get_include_path();//output:test2:test1:test3// my.phpset_include_path(get_include_path().PATH_SEPARATOR."test4:test5:test6");echo get_include_path();//output:test2:test1:test3:test4:test5:test6
1 0