Heim  >  Artikel  >  Backend-Entwicklung  >  set_include_path用法

set_include_path用法

WBOY
WBOYOriginal
2016-07-29 09:10:281125Durchsuche

转自:http://blog.sina.com.cn/s/blog_4ce89f200100twbl.html

   http://blog.sina.com.cn/s/blog_815611fb0101cqgy.html

今天在公司研习源码时,看到了set_include_path(dirname(__FILE__));这样一行代码,甚为不解。于是我就百度了一下,顺便看了一下php.net的手册,可是还是不太明白它到底有什么作用。
   偶然见看到一段另外一段代码,然后突发灵感,测试了一下,果然不出所料!
现在我总结一下吧:
   首先set_include_path这个函数呢,是在脚本里动态地对PHP.ini中include_path进行修改的。
而这个include_path呢,它可以针对下面的include和require的路径范围进行限定,或者说是预定义一下。
就好像:
       如果我们没有设置这个值,可能我们需要写一些完全的路径:

          include("123/test1.php");
          include("123/test2.php");
          include("123/test3.php");
          require("123/test4.php");
          require("123/test5.php");
       ?>
来引入很多外部文件,但是如果我们设置了set_include_path("123/"),我们就可以用下面这段代码代替。

          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;它可以允许随便去一个引入文件。
    如果再加载了许多文件夹的话,我们直接写文件名就可以了!
   但是正如我最开始那个问题一样,为什么我们公司的代码和人家的就不一样呢,原来,如果我就写一个
          set_include_path(dirname(__FILE__));
   然后去引入其他文件夹的文件,就会报错,说在我指定的这个文件夹内找不到。
      首先,我们先用另外一种方法输出一下:

           set_include_path(dirname(__FILE__));
           $include_value = ini_get('include_path');
           echo $include_value;
       ?>
结果是:D:\AppServ\www
      我如果去www下找test4.php这个文件,则没有报错
      include("test4.php");
      但是我如果去找
      include("test1.php");
      就会报错:
Warning: include() [function.include]: Failed opening 'test1.php' for inclusion (include_path='D:\AppServ\www') inD:\AppServ\www\test.phpon line6
     而且我们还发现.;C:\php5\pear;已经被替换掉了。所以我们在使用的时候,如果不是仅在一个文件夹下引入文件,我们就需要在前面加上get_include_path().PATH_SEPARATOR .

     解释一下:
            get_include_path()是获取当前include_path的默认值
            PATH_SEPARATOR 是个常量,是include的路径分界符合,在window上是;在unix和Linux上是:
     最后,我还要说一下,其实我们也可以通过另外一种方法:即最原始的:
ini_set('include_path''目录名'); 
另外,需要注意的两点就是:
           如果在指定的
目录下找不到所要求包含的文件,而在当前页面目录下正好存在这个名称的文件时,则默认引入当前目录下的该文件。

以上就介绍了set_include_path用法,包括了require方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:学习中的蜕变Nächster Artikel:收获之旅中的心路历程