• 【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:php5pear;core/;app/;admin/;lib/;include/;data/;cache/
        哎,我们发现前面还有个.;C:php5pear;这到底是怎么回事呢,其实我们如果什么也不写直接先输出一下include_path的默认值,就会发现它就是.;C:php5pear;它可以允许随便去一个引入文件。
        如果再加载了许多文件夹的话,我们直接写文件名就可以了!

  • 相关阅读:
    css基础--Display(显示) and Visibility(可见性)and position (定位)
    css3基础--Margin(外边距)&&padding(外边距)
    前端求职-js
    前端求职-html&css
    css基础3--box module&Border
    css基础-font&link&list属性
    值得学习的PHP
    c++构造函数浅析
    变量定义原则
    函数
  • 原文地址:https://www.cnblogs.com/hanyouchun/p/4252202.html
Copyright © 2020-2023  润新知