搜尋
首頁php教程php手册php自动加载类 设置包含目录 随便new也不出错

php自动加载类 设置包含目录 随便new也不出错

Jun 13, 2016 am 10:56 AM
getincludenewpathphp出錯載入包含目錄類別自動獲取設定

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拉!!

 

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),