国产精品免费无遮挡无码永久视频-国产高潮视频在线观看-精品久久国产字幕高潮-国产精品99精品无码视亚

php 自動加載類 設置包含目錄 隨便new也不出錯

發布時間:2013-9-28 18:13    發布者:reggae
關鍵詞: 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這個函數呢,是在腳本里動態地對PHP.ini中include_path進行修改的。
而這個include_path呢,它可以針對下面的include和require的路徑范圍進行限定,或者說是預定義一下。
就好像:
如果我們沒有設置這個值,可能我們需要寫一些完全的路徑:

[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.        ?>
復制代碼

來引入很多外部文件,但是如果我們設置了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.        ?>    那么這個函數它不僅可以定義一個文件夾,我們可以定義很多文件夾。如下所示,我要寫一個初始化函數:
  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/


下面呢來一個實例.

[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 '類 '.$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 '類 '.$className;
  25. include strtolower($className).".class.php";
  26. }
  27. $Smarty = new Smarty;
  28. ?>
復制代碼

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

這樣就可以 直接new拉!!

本文地址:http://www.4huy16.com/thread-121475-1-1.html     【打印本頁】

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

廠商推薦

  • Microchip視頻專區
  • “芯”光璀璨,鵬城共賞——2025 Microchip中國技術精英年會深圳站回顧
  • Microchip第22屆中國技術精英年會上海首站開幕
  • 電動兩輪車設計生態系統
  • 技術熱潮席卷三城,2025 Microchip中國技術精英年會圓滿收官!
  • 貿澤電子(Mouser)專區
關于我們  -  服務條款  -  使用指南  -  站點地圖  -  友情鏈接  -  聯系我們
電子工程網 © 版權所有   京ICP備16069177號 | 京公網安備11010502021702
快速回復 返回頂部 返回列表