PHP包含文件---set_include_path

今天用的set_include_path和get_include_path,剛開始有些困惑,總結查詢到的資料,獲得以下結論:
php

1. get_include_path windows

該函數用於獲取當前include_path 的值 bash

string get_include_path ( void ) app

例如windos下使用XAMPP輸出:.;D:\xampp\php\PEAR
函數

2. set_include_path


set_include_path()函數在腳本里動態地對PHP.iniinclude_path修改。 性能

include_path能夠針對include和require的路徑範圍進行限定(預約義)。 優化

(1)若沒設置include_path值,則須要寫徹底的路徑,可是這樣這樣會引入不少外部文件 ui

例如: spa

include('123/test1.php'); include('123/test2.php');


(2)若設置include_path可用下面代碼代替。 unix

set_include_path('123/'); include('test1.php'); include('test2.php');

由於,執行include或require時,會去include_path指定路徑查找要引入文件,雖然不知道性能是否優化,但能夠節省代碼。固然,還有玄機!

(3)這個函數不只可定義一個文件夾,還可定義不少文件夾。以下,寫一個初始化函數:

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;是php.ini中include_path的默認值。按照這個思路,若是加載許多文件夾的話,就能夠直接寫文件名了。

同時設置多個include_path時能夠用PHP常量PATH_SEPARATOR來分割。在類unix的系統中,PATH_SEPARATOR是 ":";在windows系統中,PATH_SEPARATOR的值是";";

示例:

set_include_path(implode(PATH_SEPARATOR, array(  realpath(APPLICATION_PATH . '/../lib'),  get_include_path(), )));



(4)當指定一個目錄爲include_path時,可是當lib目錄下找不到所要求包含的文件,而在當前頁面目錄下正好存在這個名稱的文件時,則轉爲包含當前目錄下的該文件。

示例:

set_include_path('projectName/home/Action/lib');require('a.php');

(5)當指定了多個目錄爲 include_path ,而所要求包含的文件在這幾個目錄都有相同名稱的文件存在時,php選擇使用設定 include_path 時排位居前的目錄下的文件。

相關文章
相關標籤/搜索