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