【php】set_include_path和get_include_path用法详解

目的:在框架中方便加载文件

参考:http://blog.sina.com.cn/s/blog_4ce89f200100twbl.html

 如果我们没有设置这个值,可能我们需要写一些完全的路径:
       <?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");
       ?>
      因为呢,当执行include或者require操作时,就会去include_path指定的路径去查找要引入的文件,虽然我现在不知道这样会不会在性能上有所优化,但是可以肯定的是,可以节省一部分代码。呵呵~
     那么刚开始的时候,我以为它加不加都是一样的没什么不同,是因为我只包含了一个本文件夹下的文件。
    后来,终于发现了其中的玄机!可恨的是,网上居然没有一篇像我这样的文章....都是同一篇文章转来转去的。
    那么这个函数它不仅可以定义一个文件夹,我们可以定义很多文件夹。如下所示,我要写一个初始化函数:
       function initialize()
{
    set_include_path(get_include_path().PATH_SEPARATOR . "core/");
    set_include_path(get_include_path().PATH_SEPARATOR . "app/");
    set_include_path(get_include_path().PATH_SEPARATOR . "admin/");
    set_include_path(get_include_path().PATH_SEPARATOR . "lib/");
    set_include_path(get_include_path().PATH_SEPARATOR . "include/");
    set_include_path(get_include_path().PATH_SEPARATOR."data/");
    set_include_path(get_include_path().PATH_SEPARATOR."cache/");
}
    这样它的路径就成了:
    .;C:\php5\pear;core/;app/;admin/;lib/;include/;data/;cache/
    哎,我们发现前面还有个.;C:\php5\pear;这到底是怎么回事呢,其实我们如果什么也不写直接先输出一下include_path的默认值,就会发现它就是.;C:\php5\pear;它可以允许随便去一个引入文件。
    如果再加载了许多文件夹的话,我们直接写文件名就可以了!

posted @ 2015-01-27 10:53  悲惨的大爷  阅读(1267)  评论(0编辑  收藏  举报