【php】set_include_path和get_include_path用法詳解

來源:互聯網
上載者:User

標籤:

目的:在架構中方便負載檔案

參考: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;它可以允許隨便去一個引入檔案。
    如果再載入了許多檔案夾的話,我們直接寫檔案名稱就可以了!

【php】set_include_path和get_include_path用法詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.