php 自動(dòng)加載類(lèi) 設置包含目錄 隨便new也不出錯

發(fā)布時(shí)間:2013-9-28 18:13    發(fā)布者:reggae
關(guān)鍵詞: php
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這個(gè)函數呢,是在腳本里動(dòng)態(tài)地對PHP.ini中include_path進(jìn)行修改的。
而這個(gè)include_path呢,它可以針對下面的include和require的路徑范圍進(jìn)行限定,或者說(shuō)是預定義一下。
就好像:
如果我們沒(méi)有設置這個(gè)值,可能我們需要寫(xiě)一些完全的路徑:

[php]
  1.    include("123/test1.php");
  2.    include("123/test2.php");
  3.    include("123/test3.php");
  4.    require("123/test4.php");
  5.    require("123/test5.php");
  6. ?>

  7.       
  8.           include("123/test1.php");
  9.           include("123/test2.php");
  10.           include("123/test3.php");
  11.           require("123/test4.php");
  12.           require("123/test5.php");
  13.        ?>
復制代碼

來(lái)引入很多外部文件,但是如果我們設置了set_include_path("123/"),我們就可以用下面這段代碼代替。
[php]
  1.    set_include_path("123/");
  2.    include("test1.php");
  3.    include("test2.php");
  4.    include("test3.php");
  5.    require("test4.php");
  6.    require("test5.php");
  7. ?>

  8.       
  9.           set_include_path("123/");
  10.           include("test1.php");
  11.           include("test2.php");
  12.           include("test3.php");
  13.           require("test4.php");
  14.           require("test5.php");
  15.        ?>    那么這個(gè)函數它不僅可以定義一個(gè)文件夾,我們可以定義很多文件夾。如下所示,我要寫(xiě)一個(gè)初始化函數:
  16. [php]
  17.   function initialize()

  18. set_include_path(get_include_path().PATH_SEPARATOR . "core/");
  19. set_include_path(get_include_path().PATH_SEPARATOR . "app/");
  20. set_include_path(get_include_path().PATH_SEPARATOR . "admin/");
  21. set_include_path(get_include_path().PATH_SEPARATOR . "lib/");
  22. set_include_path(get_include_path().PATH_SEPARATOR . "include/");
  23. set_include_path(get_include_path().PATH_SEPARATOR."data/");
  24. set_include_path(get_include_path().PATH_SEPARATOR."cache/");

  25.        function initialize()
  26. {
  27.     set_include_path(get_include_path().PATH_SEPARATOR . "core/");
  28.     set_include_path(get_include_path().PATH_SEPARATOR . "app/");
  29.     set_include_path(get_include_path().PATH_SEPARATOR . "admin/");
  30.     set_include_path(get_include_path().PATH_SEPARATOR . "lib/");
  31.     set_include_path(get_include_path().PATH_SEPARATOR . "include/");
  32.     set_include_path(get_include_path().PATH_SEPARATOR."data/");
  33.     set_include_path(get_include_path().PATH_SEPARATOR."cache/");
  34. }   
復制代碼

這樣它的路徑就成了:




.;C:\php5\pear;core/;app/;admin/;lib/;include/;data/;cache/


下面呢來(lái)一個(gè)實(shí)例.

[php]
  1. $include_path=get_include_path();                         //原基目錄  
  2. $include_path.=PATH_SEPARATOR."include/" ;
  3. $include_path.=PATH_SEPARATOR."classs/";
  4. $include_path.=PATH_SEPARATOR."libs/";
  5. //echo $include_path;  
  6. //設置include包含文件所在的所有目錄   
  7. set_include_path($include_path);

  8. function __autoload($className)
  9. {
  10. //echo '類(lèi) '.$className;  
  11. include strtolower($className).".class.php";
  12. }
  13. $Smarty = new Smarty;
  14. ?>

  15. $include_path=get_include_path();                         //原基目錄
  16. $include_path.=PATH_SEPARATOR."include/" ;
  17. $include_path.=PATH_SEPARATOR."classs/";
  18. $include_path.=PATH_SEPARATOR."libs/";
  19. //echo $include_path;
  20. //設置include包含文件所在的所有目錄
  21. set_include_path($include_path);

  22. function __autoload($className)
  23. {
  24. //echo '類(lèi) '.$className;
  25. include strtolower($className).".class.php";
  26. }
  27. $Smarty = new Smarty;
  28. ?>
復制代碼

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

這樣就可以 直接new拉!!

本文地址:http://selenalain.com/thread-121475-1-1.html     【打印本頁(yè)】

本站部分文章為轉載或網(wǎng)友發(fā)布,目的在于傳遞和分享信息,并不代表本網(wǎng)贊同其觀(guān)點(diǎn)和對其真實(shí)性負責;文章版權歸原作者及原出處所有,如涉及作品內容、版權和其它問(wèn)題,我們將根據著(zhù)作權人的要求,第一時(shí)間更正或刪除。
您需要登錄后才可以發(fā)表評論 登錄 | 立即注冊

關(guān)于我們  -  服務(wù)條款  -  使用指南  -  站點(diǎn)地圖  -  友情鏈接  -  聯(lián)系我們
電子工程網(wǎng) © 版權所有   京ICP備16069177號 | 京公網(wǎng)安備11010502021702
快速回復 返回頂部 返回列表
午夜高清国产拍精品福利|亚洲色精品88色婷婷七月丁香|91久久精品无码一区|99久久国语露脸精品|动漫卡通亚洲综合专区48页