php 自動(dòng)加載類 設(shè)置包含目錄 隨便new也不出錯(cuò)
發(fā)布時(shí)間:2013-9-28 18:13
發(fā)布者:
reggae
get_include_path — 獲取當(dāng)前的 include_path 配置選項(xiàng)
string get_include_path ( void )
set_include_path — 設(shè)置 include_path 配置選項(xiàng)
string set_include_path ( string $new_include_path )
首先set_include_path這個(gè)函數(shù)呢,是在腳本里動(dòng)態(tài)地對(duì)PHP.ini中include_path進(jìn)行修改的。
而這個(gè)include_path呢,它可以針對(duì)下面的include和require的路徑范圍進(jìn)行限定,或者說(shuō)是預(yù)定義一下。
就好像:
如果我們沒(méi)有設(shè)置這個(gè)值,可能我們需要寫一些完全的路徑:
[php]
-
- include("123/test1.php");
- include("123/test2.php");
- include("123/test3.php");
- require("123/test4.php");
- require("123/test5.php");
- ?>
-
- include("123/test1.php");
- include("123/test2.php");
- include("123/test3.php");
- require("123/test4.php");
- require("123/test5.php");
- ?>
復(fù)制代碼
來(lái)引入很多外部文件,但是如果我們?cè)O(shè)置了set_include_path("123/"),我們就可以用下面這段代碼代替。
[php]
-
- set_include_path("123/");
- include("test1.php");
- include("test2.php");
- include("test3.php");
- require("test4.php");
- require("test5.php");
- ?>
-
- set_include_path("123/");
- include("test1.php");
- include("test2.php");
- include("test3.php");
- require("test4.php");
- require("test5.php");
- ?> 那么這個(gè)函數(shù)它不僅可以定義一個(gè)文件夾,我們可以定義很多文件夾。如下所示,我要寫一個(gè)初始化函數(shù):
- [php]
- 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/");
- 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/");
- }
復(fù)制代碼
這樣它的路徑就成了:
.;C:\php5\pear;core/;app/;admin/;lib/;include/;data/;cache/
下面呢來(lái)一個(gè)實(shí)例.
[php]
-
- $include_path=get_include_path(); //原基目錄
- $include_path.=PATH_SEPARATOR."include/" ;
- $include_path.=PATH_SEPARATOR."classs/";
- $include_path.=PATH_SEPARATOR."libs/";
- //echo $include_path;
- //設(shè)置include包含文件所在的所有目錄
- set_include_path($include_path);
-
- function __autoload($className)
- {
- //echo '類 '.$className;
- include strtolower($className).".class.php";
- }
- $Smarty = new Smarty;
- ?>
-
- $include_path=get_include_path(); //原基目錄
- $include_path.=PATH_SEPARATOR."include/" ;
- $include_path.=PATH_SEPARATOR."classs/";
- $include_path.=PATH_SEPARATOR."libs/";
- //echo $include_path;
- //設(shè)置include包含文件所在的所有目錄
- set_include_path($include_path);
- function __autoload($className)
- {
- //echo '類 '.$className;
- include strtolower($className).".class.php";
- }
- $Smarty = new Smarty;
- ?>
復(fù)制代碼
當(dāng)指定了多個(gè)目錄為 include_path ,而所要求包含的文件在這幾個(gè)目錄都有相同名稱的文件存在時(shí),php選擇使用設(shè)定 include_path 時(shí)排位居前的目錄下的文件。
這樣就可以 直接new拉!!
|