set_include_path和get_include_path用法详解

今天在看小猪cms源码时看到了下面这段代码,表示很疑惑

set_include_path(get_include_path() . PATH_SEPARATOR . VENDOR_PATH);

 VENDOR_PATH 为框架自定义的常量 表示一个第三方类库目录 剩下的就不知道了

于是就百度下 发现了其中奥妙之处

首先set_include_path这个函数呢,是在脚本里动态地对PHP.ini中include_path进行修改的。
而这个include_path呢,它可以针对下面的include和require的路径范围进行限定,或者说是预定义一下。
 

就好像:
       如果我们没有设置这个值,可能我们需要写一些完全的路径:
  
    <?php
          include("123/test1.php");
          include("123/test2.php");
          include("123/test3.php");
          require("123/test4.php");
          require("123/test5.php");
       ?>
来引入很多外部文件,但是如果我们设置了set_include_path("123/"),我们就可以用下面这段代码代替。
       <?php
          set_include_path("123/");
          include("test1.php");
          include("test2.php");
          include("test3.php");
          require("test4.php");
          require("test5.php");
       ?>

 get_include_path()是获取当前include_path的默认值
 PATH_SEPARATOR 是个常量,是include的路径分界符合,在window上是;在unix和Linux上是:
 最后,我还要说一下,其实我们也可以通过另外一种方法:即最原始的:
ini_set('include_path''目录名');

当指定一个目录为include_path时,但是当设置的include_path目录下找不到所要求包含的文件,而在当前页面目录下正好存在这个名称的文件时,则转为包含当前目录下的该文件。

posted @ 2017-02-17 14:51  ー個亽の江湖  阅读(252)  评论(0编辑  收藏  举报