zoukankan      html  css  js  c++  java
  • 【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;它可以允许随便去一个引入文件。
        如果再加载了许多文件夹的话,我们直接写文件名就可以了!

  • 相关阅读:
    关于js计算非等宽字体宽度的方法
    [NodeJs系列]聊一聊BOM
    Vue.js路由管理器 Vue Router
    vue 实践技巧合集
    微任务、宏任务与Event-Loop
    事件循环(EventLoop)的学习总结
    Cookie、Session和LocalStorage
    MySQL 树形结构 根据指定节点 获取其所在全路径节点序列
    MySQL 树形结构 根据指定节点 获取其所有父节点序列
    MySQL 创建函数报错 This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators
  • 原文地址:https://www.cnblogs.com/hanyouchun/p/4252202.html
Copyright © 2011-2022 走看看