標籤:
目的:在架構中方便負載檔案
參考: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用法詳解