首页 > 代码库 > PHP中使用set_include_path动态设置文件加载扫描路径

PHP中使用set_include_path动态设置文件加载扫描路径

/index.php
/include/config.php

下面是index.php

<?php 
$path = ‘/usr/lib/pear‘; 
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
//设置后的include_path变为类似/usr/lib/function;/usr/lib/pear 
include("config.php"); //先搜索当前目录->搜索get_include_path()列出的路径
?>

输出一下

<?php 
  echo get_include_path();
?>

结果

/user/tpls;/usr/lib/function;/usr/lib/pear;

这类似于java中的类加载路径,加载一个文件时,在扫描当前目录之后,将在上述3个路径下查找。

PHP中使用set_include_path动态设置文件加载扫描路径