include_path是PHP中的一个环境变量,在php.ini中初始化设置
例如:有些如下的文件
test/a.php
test/b.php
test/test2/c.php
test/test2/d.php
如果没有设置include_path变量,index.php需要这样写:
<?php
include_once 'test/a.php';
include_once 'test/b.php';
include_once 'test/test2/c.php';
include_once 'test/test2/d.php';
?>
使用上面的引用方式,引用路径比较长,当引用目录位置改变时需要修改大量代码。使用include_path可以简化上述问题
PATH_SEPARATOR是一个常量,在Linux系、Mac系统是一个”:”,
在Windlows上是一个”;”号。
<?php
set_include_path("test".PATH+SEPARATOR."test/test2");
include_once 'a.php';
include_once 'b.php';
include_once 'c.php';
include_once 'd.php';
?>
include_path是PHP的环境变量,因而可以在php.ini设置,每次请求时include_path都会被PHP用php.ini中的值初始化。也可以用代码的方式修改include_path值,不过,修改后结果在请求完毕后会自动丢弃,不会带到下一个请求。因此,每次请求时需要重新设置。
在代码中获取和设置include_path值有如下两种方式:
方式一:ini_get()和ini_set()方式,此种方式使用于所有PHP版本。
<?php
$s = ini_get('include_path');
ini_set($s . PATH_SEPARATOR . 'test'.PATH_SEPARATOR . 'test/test2');
?>
方式二:get_include_path()和set_include_path()方式,此种方式使用于PHP4.3以后的版本。
<?php
$s = get_include_path();
set_include_path($s . PATH_SEPARATOR . 'test'.PATH_SEPARATOR . 'test/test2');
?>