set_include_path() &&get_include_path()用法

來源:互聯網
上載者:User

標籤:http   io   os   使用   ar   strong   for   檔案   sp   

       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__));
   然後去引入其他檔案夾的檔案,就會報錯,說在我指定的這個檔案夾內找不到。
      首先,我們先用另外一種方法輸出一下:
       <?php
           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‘) in D:\AppServ\www\test.php on line 6
     而且我們還發現
.;C:\php5\pear;已經被替換掉了。所以我們在使用的時候,如果不是僅在一個檔案夾下引入檔案,我們就需要在前面加上get_include_path().PATH_SEPARATOR .
     解釋一下:
            get_include_path()是擷取當前include_path的預設值
            PATH_SEPARATOR 是個常量,是include的路徑分界符合,在window上是;在unix和Linux上是:
     最後,我還要說一下,其實我們也可以通過另外一種方法:即最原始的:
           ini_set(‘include_path‘, ‘目錄名‘); 
    另外,需要注意的兩點就是:
           如果在指定的目錄下找不到所要求包含的檔案,而在當前頁面目錄下正好存在這個名稱的檔案時,則預設引入目前的目錄下的該檔案。
           當指定了多個目錄為 include_path ,而所要求包含的檔案在這幾個目錄都有相同名稱的檔案存在時,php選擇使用設定 include_path 時排位居前的目錄下的檔案。

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.