国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Backend Development PHP Tutorial 文件加載-了解一個project的第一步

文件加載-了解一個project的第一步

Jun 13, 2016 pm 12:15 PM
autoload include nbsp php

文件加載---理解一個project的第一步

  當(dāng)我最開始寫php的時候,總是擔(dān)心這個問題:我在這兒new的一個class能加載到對應(yīng)的類文件嗎?畢竟一運行就報Fatal Error,什么**文件沒找到,類無法實例化等等是一種很“低級”的錯誤,怕別人看了笑話。于是每接一個新任務(wù),我總想把它的加載過程弄清楚(以前只知道幾個html標(biāo)簽和樣式,不知算不算web開發(fā)),有時頭兒看了說還有閑心看這個,趕緊寫邏輯,照這樣做就行了......你妹你知道當(dāng)然有把握了D:,后來發(fā)現(xiàn)原來流程都差不多。

  在一個IDE中開發(fā)時,如C++/Java,一般是新建一個工程,通過IDE新添加一個文件到指定目錄下,然后#include/Import進(jìn)來即可,php則使這一步驟更加過程化,文件的加載過程基本確定了這個project(框架或者自搭的項目)的目錄結(jié)構(gòu)和文件的分門別類。

   不管框架還是自搭的項目總得有個入口文件,這時要事先加載一些基本信息,如配置文件、通用方法等,使用的基本是手動直接加載單個文件形式,使用下面四個方法之一:

  include、require、include_once、require_once

    <span style="color: #0000ff;">include</span>('config.php'<span style="color: #000000;">);    </span><span style="color: #0000ff;">require</span>('database.php');

  涉及到類文件的加載,少部分是直接加載,比如,通用方法作為靜態(tài)方法寫在一個類Utilities中,因為是后邊很多要用到的方法(如錯誤輸出、curl請求、隨機字符串生成...),所以用類封裝起來,一般也是在加載配置文件時連帶加載進(jìn)來

  include('Utilities.php');

  而更通用的情況是:類的動態(tài)加載。首先不談的加載的方式,來看看大概什么時候會用到一個類和實例:

  1. 最明顯的,$obj = new A; ?它的變種$className = 'A'; $obj = $className; 都一樣;

  2. 類的靜態(tài)方法、靜態(tài)變量和常量的調(diào)用,即Utilities::httpRequest()、Utilities::$instance、Utilities::HOST;

  3. 在php函數(shù)中,使用了回調(diào)函數(shù)的情況,最典型的call_user_func_array()(call_user_func),還有其他用到了callback的地方,如數(shù)組中的array_walk、array_map,它們需要一個回調(diào)函數(shù)作為參數(shù)。

  回調(diào)函數(shù)非常靈活,不止可以是簡單函數(shù),還可以是對象的方法,包括靜態(tài)類方法。因為可以用對象方法或靜態(tài)方法,所以這鐘時候也要去加載對應(yīng)的類文件。自php5.3起,回調(diào)函數(shù)還可以像js中,用匿名函數(shù)來實現(xiàn)。

     <span style="color: #0000ff;">class</span><span style="color: #000000;"> A{         </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> cube(<span style="color: #800080;">$var</span><span style="color: #000000;">){             </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">pow</span>(<span style="color: #800080;">$var</span>, 3<span style="color: #000000;">);         }                  </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> twice(<span style="color: #800080;">$var</span><span style="color: #000000;">){             </span><span style="color: #0000ff;">return</span> 2*<span style="color: #800080;">$var</span><span style="color: #000000;">;         }     }     </span><span style="color: #008000;">//</span><span style="color: #008000;"> 使用類的靜態(tài)方法</span>     <span style="color: #800080;">$num</span> = <span style="color: #008080;">call_user_func</span>('A::cube', 5<span style="color: #000000;">);     </span><span style="color: #008000;">//</span><span style="color: #008000;"> 使用對象</span>     <span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> A;     </span><span style="color: #800080;">$num</span> = <span style="color: #008080;">call_user_func_array</span>(<span style="color: #0000ff;">array</span>(<span style="color: #800080;">$obj</span>, 'twice'), <span style="color: #0000ff;">array</span>(7));

  嚴(yán)格來說上例中的call_user_func_array在之前已經(jīng)實例化了對象,但是存在這么個用法,它完全也可以使用類靜態(tài)方法。

  首先要明白的是,為什么需要動態(tài)加載。php是腳本語言,我們訪問時,是以腳本為可用資源,比如現(xiàn)在根目錄有個index.php文件,它沒有include任何其他文件,當(dāng)我們直接以localhost/index.php來訪問時,可以訪問到index.php中的全部資源,如果index.php中定義了一個普通類A,在該腳本中實例化一個A的對象時,程序會這樣反應(yīng):哦,我已經(jīng)看到了A的定義,可以直接實例化它(不需要加載其他文件)。如果還有類B、C、D等很多類,全部寫在index.php中顯然不行,那就寫在其他文件中,再include進(jìn)來(include已經(jīng)在做加載的工作了),這樣對程序來說,也是“可見”的了。

  但是隨著系統(tǒng)功能的增多,類越來越多,各個類的功能也不同,有的直接定義數(shù)據(jù)庫的操作,讀取數(shù)據(jù)庫的數(shù)據(jù),有的是控制訪問腳本時要運行的方法,有的則是將要展現(xiàn)出來的頁面,有的是我們引用的第三方核心庫,于是,當(dāng)我們把所有的文件放在一個目錄中時,雖然可以直接include加載,但這些文件擺放顯得既雜亂無章又難找,維護(hù)成本還高。好唄,那就在根目錄下再分別建幾個目錄,目錄A專門存放與數(shù)據(jù)庫打交道的腳本,目錄B是系統(tǒng)的各種配置信息文件,目錄C是控制我們進(jìn)入程序時的入口控制方法的腳本,目錄D是即將展示到瀏覽器的頁面......

  于是MVC架構(gòu)慢慢就演化出來了,我們不能再像以前那樣直接include,腳本都放在特定的目錄下,如Controller目錄下存放的是各種控制器,加載控制器時,我們得這樣include('root/Controller/indexController.php'),每次都在文件前面弄一大串的include不僅看著頭疼,簡直讓人累覺不愛。既然有了獲取當(dāng)前文件路徑和類名的現(xiàn)成方法,為何不將類名與文件名對應(yīng)起來,而只要是控制器類的腳本就全放在根目錄的Controller子目錄下邊,就可以寫一個方法,只要是控制器類,在這個方法中運行include(ROOT.'Controller/'.$className.'.php');這一句,ROOT為根目錄常量,$className為傳入的類名,只要是模型類,就這樣include(ROOT.'Model/'.$className.'.php');,全憑這個函數(shù)來動態(tài)控制到哪個目錄里邊去找,這個project可能就是這樣的:

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

  無形中,就建立起了類名和文件名的對應(yīng)規(guī)則,文件和所在的目錄的對應(yīng)規(guī)則,該project下有哪些這樣的目錄和文件呢?啊原來是放控制器的Controller、放配置信息的Config等等,再次于無形中得知了這個project的結(jié)構(gòu),而上面說的,利用函數(shù)根據(jù)一定條件(傳入?yún)?shù))可知自動到哪個目錄下去加載該文件,而不是一個個寫死的include,就是所謂的文件的動態(tài)加載了。

  因此,當(dāng)你要新建一個**類文件時,也就知道,哦在這個project中,我應(yīng)該放在這個目錄下,文件的命名應(yīng)該與類名相同,這樣就一定能加載到了~~~接下來就是寫業(yè)務(wù)邏輯的一個“愉快的過程”。

  知道什么時候會動態(tài)加載及為什么要動態(tài)加載后,接下來就是來實現(xiàn)了,也就是上面說到的利用函數(shù)來加載某個文件,就是要寫好這個“函數(shù)”來實現(xiàn)這個過程。常用的有三種方式:

  1. __autoload

  我第一次學(xué)的時候就是用的就是這個,魔術(shù)函數(shù),只要定義了php程序就會在要用到一個類時自動調(diào)用它進(jìn)行文件動態(tài)加載,一樣,既然它是個函數(shù),就要讓程序?qū)_autoload的定義可見,不然從哪兒調(diào)用它呢?一般來說,作為后邊程序大部分地方要用到的方法,我們都會放在一個單獨的文件中,在程序的入口處加載進(jìn)來,一個project總得有幾個文件是手動include的,完全可以在開頭單獨include進(jìn)來,或者放在配置信息中,加載配置信息時就加載進(jìn)來了。它的原型:

  void?__autoload?(?string $class?)

  參數(shù)當(dāng)前加載的類名名稱(注意如果有命名空間,則包含命名空間前綴),下面是一個針對上面的圖片結(jié)構(gòu)的簡單示例:

    <span style="color: #008000;">//</span><span style="color: #008000;"> file: autoload.php     // ROOT為已經(jīng)定義的根目錄常量</span>    <span style="color: #0000ff;">function</span> __autoload(<span style="color: #800080;">$className</span><span style="color: #000000;">){        </span><span style="color: #0000ff;">try</span><span style="color: #000000;">{            </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">file_exists</span>(ROOT.'Controller/'.<span style="color: #800080;">$className</span>.'.php')){<span style="color: #008000;">//</span><span style="color: #008000;"> 檢查Controller</span>                <span style="color: #0000ff;">include</span>(ROOT.'Controller/'.<span style="color: #800080;">$className</span>.'.php'<span style="color: #000000;">);            }            </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(<span style="color: #008080;">file_exists</span>(ROOT.'Model/'.<span style="color: #800080;">$className</span>.'.php')){<span style="color: #008000;">//</span><span style="color: #008000;"> 檢查Model</span>                <span style="color: #0000ff;">include</span>(ROOT.'Model/'.<span style="color: #800080;">$className</span>.'.php'<span style="color: #000000;">);            }            </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(<span style="color: #008080;">file_exists</span>(ROOT.'Lib/'.<span style="color: #800080;">$className</span>.'.php')){<span style="color: #008000;">//</span><span style="color: #008000;"> 檢查Lib</span>                <span style="color: #0000ff;">include</span>(ROOT.'Lib/'.<span style="color: #800080;">$className</span>.'.php'<span style="color: #000000;">);            }            </span><span style="color: #0000ff;">else</span>{                                               <span style="color: #008000;">//</span><span style="color: #008000;"> 找不到該文件</span>                <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">Exception</span>("ERROR: can't find file {<span style="color: #800080;">$className</span>}.php"<span style="color: #000000;">);            }        }        </span><span style="color: #0000ff;">catch</span>(<span style="color: #0000ff;">Exception</span> <span style="color: #800080;">$e</span><span style="color: #000000;">){            </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$e</span>.<span style="color: #000000;">getMessage();            </span><span style="color: #0000ff;">exit</span><span style="color: #000000;">;        }    }</span>

?  

  2. spl_autoload_register

  __autoload實際上也差不多了,但它是php定義的,如果現(xiàn)在有個東西寫了并調(diào)用之后,就告訴程序說,我不用__autoload來加載文件了,我已經(jīng)定義了一個專門加載文件的方法(比如名稱是loadClass),以后需要加載一個類文件時,你就用它吧。spl_autoload_register就是這樣一個能告訴程序這樣去做的方法,而且自定義加載方法將會更靈活,可以指定多個加載函數(shù),spl_autoload_register函數(shù)會將這些函數(shù)放在一個隊列中,并激活它們,在調(diào)用時逐個激活:“If there must be multiple autoload functions,?spl_autoload_register()?allows for this. It effectively creates a queue of autoload functions, and runs through each of them in the order they are defined. ”,php.net上(http://php.net/manual/en/function.spl-autoload-register.php)也確實如此解釋,spl_autoload_unregister則是從加載函數(shù)隊列中注銷。

  另外spl_autoload_functions()函數(shù),可以獲取我們注冊了哪些函數(shù);spl_autoload_call($class)函數(shù),嘗試調(diào)用所有已注冊的加載函數(shù)來加載$class的類文件。

  對于spl_autoload_register的解釋,我的理解是,如果用spl_autoload_register注冊了n個函數(shù)在加載隊列中,因為它自動激活它們嘛,現(xiàn)在我要實例化一個類,在第1個加載函數(shù)中加載失敗了,然后嘗試第2個函數(shù),第二個失敗則嘗試第3個,''',直到第n個函數(shù)走完,若還沒加載成功,就報錯,只要中間一個加載成功就成功了,but事實好像有點出入。

  還是用上一個圖片中的目錄結(jié)構(gòu),

  1、在Controller目下創(chuàng)建indexController.php文件,包含類indexController;

  2、在Model目錄下創(chuàng)建userModel.php文件,包含類userModel;

  3、首頁寫個類加載腳本Autoload.php,代碼如下:

    <span style="color: #008000;">//</span><span style="color: #008000;"> file: Autoload.php</span>    <span style="color: #008080;">define</span>('DS',<span style="color: #000000;"> DIRECTORY_SEPARATOR);    </span><span style="color: #008080;">define</span>('ROOT', <span style="color: #008080;">rtrim</span>(<span style="color: #008080;">dirname</span>(<span style="color: #ff00ff;">__FILE__</span>), '/\\').<span style="color: #000000;">DS);        </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Autoload{        </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> autoloadRegister(<span style="color: #800080;">$loadFunc</span> = 'Autoload::loadControllerClass', <span style="color: #800080;">$enable</span> = <span style="color: #0000ff;">true</span><span style="color: #000000;">){            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$enable</span> ? spl_autoload_register(<span style="color: #800080;">$loadFunc</span>) : spl_autoload_unregister(<span style="color: #800080;">$loadFunc</span><span style="color: #000000;">);        }        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 加載控制器類</span>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> loadControllerClass(<span style="color: #800080;">$className</span><span style="color: #000000;">){            </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">file_exists</span>(ROOT.'Controller'.DS.<span style="color: #800080;">$className</span>.'.php')){<span style="color: #008000;">//</span><span style="color: #008000;"> 檢查Controller</span>                <span style="color: #0000ff;">include</span>(ROOT.'Controller'.DS.<span style="color: #800080;">$className</span>.'.php'<span style="color: #000000;">);                </span><span style="color: #0000ff;">echo</span> ROOT.'Controller'.DS.<span style="color: #800080;">$className</span>.'.php'.'<br>'<span style="color: #000000;">;            }            </span><span style="color: #0000ff;">else</span><span style="color: #000000;">{                </span><span style="color: #0000ff;">echo</span> "ERROR: can't find file {<span style="color: #800080;">$className</span>}.php in ".ROOT."Controller"<span style="color: #000000;">;                </span><span style="color: #0000ff;">exit</span><span style="color: #000000;">;            }        }        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 加載模型類</span>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> loadModelClass(<span style="color: #800080;">$className</span><span style="color: #000000;">){            </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">file_exists</span>(ROOT.'Model'.DS.<span style="color: #800080;">$className</span>.'.php')){<span style="color: #008000;">//</span><span style="color: #008000;"> 檢查Model</span>                <span style="color: #0000ff;">include</span>(ROOT.'Model'.DS.<span style="color: #800080;">$className</span>.'.php'<span style="color: #000000;">);                </span><span style="color: #0000ff;">echo</span> ROOT.'Model'.DS.<span style="color: #800080;">$className</span>.'.php'.'<br>'<span style="color: #000000;">;            }            </span><span style="color: #0000ff;">else</span><span style="color: #000000;">{                </span><span style="color: #0000ff;">echo</span> "ERROR: can't find file {<span style="color: #800080;">$className</span>}.php in ".ROOT."Model"<span style="color: #000000;">;                </span><span style="color: #0000ff;">exit</span><span style="color: #000000;">;            }        }    }</span>

  4、測試腳本,測試類是否能加載

    <span style="color: #008000;">//</span><span style="color: #008000;"> 注冊兩個加載函數(shù)</span>    Autoload::autoloadRegister('Autoload::loadControllerClass'<span style="color: #000000;">);    Autoload</span>::autoloadRegister('Autoload::loadModelClass'<span style="color: #000000;">);</span><span style="color: #008000;">        // 查看總共注冊了哪些加載函數(shù)</span>    <span style="color: #0000ff;">echo</span> 'register functions=> <pre class="brush:php;toolbar:false">'<span style="color: #000000;">;    </span><span style="color: #008080;">print_r</span><span style="color: #000000;">(spl_autoload_functions());    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 分別實例化一個Controller類和Model類</span>    <span style="color: #800080;">$indexCon</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> indexController;    </span><span style="color: #800080;">$userMod</span> = <span style="color: #0000ff;">new</span> userModel;

  ?結(jié)果是這樣

  ??

  這不科學(xué)啊,spl_autoload_functions數(shù)組顯示兩個函數(shù)都注冊了,但是當(dāng)實例化userModel類時它還是跑到Controller目錄中去找,兩個類的實例化調(diào)用的自動加載方法都是Autoload::loadControllerClass,所以userModel類文件加載報錯......注意到spl_autoload_register方法的第三個參數(shù), 是添加一個加載函數(shù)時放在棧中的位置,于是我另寫一個類似的類otherLoad,只是為了將loadModelClass方法放到隊列首部:

    <span style="color: #0000ff;">class</span><span style="color: #000000;"> otherLoad{        </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> autoloadRegister(<span style="color: #800080;">$loadFunc</span> = 'otherLoad::loadModelClass', <span style="color: #800080;">$enable</span> = <span style="color: #0000ff;">true</span><span style="color: #000000;">){            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 默認(rèn)將loadModelClass放在隊首</span>            <span style="color: #0000ff;">return</span> <span style="color: #800080;">$enable</span> ? spl_autoload_register(<span style="color: #800080;">$loadFunc</span>, <span style="color: #0000ff;">true</span>, <span style="color: #0000ff;">true</span>) : spl_autoload_unregister(<span style="color: #800080;">$loadFunc</span><span style="color: #000000;">);        }        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 加載模型類</span>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> loadModelClass(<span style="color: #800080;">$className</span><span style="color: #000000;">){            </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">file_exists</span>(ROOT.'Model'.DS.<span style="color: #800080;">$className</span>.'.php')){<span style="color: #008000;">//</span><span style="color: #008000;"> 檢查Model</span>                <span style="color: #0000ff;">include</span>(ROOT.'Model'.DS.<span style="color: #800080;">$className</span>.'.php'<span style="color: #000000;">);                </span><span style="color: #0000ff;">echo</span> ROOT.'Model'.DS.<span style="color: #800080;">$className</span>.'.php'.'<br>'<span style="color: #000000;">;            }            </span><span style="color: #0000ff;">else</span><span style="color: #000000;">{                </span><span style="color: #0000ff;">echo</span> "ERROR: can't find file {<span style="color: #800080;">$className</span>}.php in ".ROOT."Model"<span style="color: #000000;">;                </span><span style="color: #0000ff;">exit</span><span style="color: #000000;">;            }        }    } </span>

  測試是這樣

    <span style="color: #008000;">//</span><span style="color: #008000;"> 注冊三個加載函數(shù)</span>    Autoload::autoloadRegister('Autoload::loadControllerClass'<span style="color: #000000;">);    Autoload</span>::autoloadRegister('Autoload::loadModelClass'<span style="color: #000000;">);    otherLoad</span>::autoloadRegister('otherLoad::loadModelClass'<span style="color: #000000;">);        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 查看總共注冊了哪些加載函數(shù)</span>    <span style="color: #0000ff;">echo</span> 'register functions=> <pre class="brush:php;toolbar:false">'<span style="color: #000000;">;    </span><span style="color: #008080;">print_r</span><span style="color: #000000;">(spl_autoload_functions());    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 分別實例化一個Controller類和Model類</span>    <span style="color: #800080;">$indexCon</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> indexController;    </span><span style="color: #800080;">$userMod</span> = <span style="color: #0000ff;">new</span> userModel;

  這次的結(jié)果是這樣:

  

  可以看到,這次是在加載indexController類時不成功,因為它只調(diào)用了loadModelClass方法,再看看spl_autoload_functions返回的數(shù)組,otherLoad類的loadModelClass方法在最前面,難道說,只有在加載函數(shù)隊列最前面的函數(shù)才被用于自動加載,其他無效?這是什么狀況?

  使用spl_autoload_call('indexController')來“嘗試調(diào)用所有已注冊的函數(shù)來裝載請求類”,還是報這個錯。

  翻了下別人的文章,包括github上的博客,也就是列舉了下手冊上說的“可以一次注冊多個加載函數(shù) bala bala......”,難道沒有人試過,還是我的理解有問題>3<...>

  關(guān)于spl_autoload_register還有幾個有意思的地方:

  1、 一個函數(shù)只會加載到函數(shù)隊列中一次,重復(fù)加載也是如此;

  2、 spl_autoload_register如果不指定加載函數(shù)(第一個參數(shù)),則默認(rèn)使用加載函數(shù)spl_autoload(功能類似于__autoload,是它的默認(rèn)實現(xiàn)形式)

  3、 spl_autoload_register指定了__autoload為加載函數(shù),則一定要實現(xiàn)__autoload;

  4、 同時實現(xiàn)了spl_autoload_register和__autoload,優(yōu)先使用spl_autoload_register注冊的加載函數(shù)。

  以上幾種情況幾乎都可從php.net的note中找到測試?yán)樱贤鈱懙猛τ幸馑?,可供參考。上面?點還需要注意,比如現(xiàn)在在根目錄創(chuàng)建一個目錄,使用默認(rèn)函數(shù)來加載:

    <span style="color: #008000;">//</span><span style="color: #008000;"> 設(shè)置加載文件的擴展名,將只加載*.php的文件</span>    spl_autoload_extensions('.php'<span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 默認(rèn)使用spl_autoload加載文件,只能加載當(dāng)前目錄下文件:小寫類名.php</span><span style="color: #000000;">    spl_autoload_register();    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 測試    // $obj = new A;</span>

  spl_autoload_extensions設(shè)置加載時只認(rèn)哪些擴展類型的文件,默認(rèn)是.php或者.inc文件,這里設(shè)置成.php,然后就是調(diào)用注冊函數(shù)。在根目錄下創(chuàng)建一個A.php文件,新建一個類A,加載成功,再將文件名改成a.php,照樣加載成功。需要留意spl_autoload默認(rèn)將類名轉(zhuǎn)小寫,但是A.php照樣加載成功,因為Windows的文件是大小寫不敏感的(在同一目錄下創(chuàng)建一個d.txt,再創(chuàng)建D.txt會認(rèn)為是同一個文件),對于Mac OS X也是這樣,但Linux就是大小寫敏感了,測試時要注意這點。

  也不是全要自動加載,如CI,它將加載文件封裝為一個核心類CI_Loader,程序啟動時先include必要的腳本(其他要用的核心類),然后再等需要使用時,CI_Loader實例作為當(dāng)前控制器類或模型類等的一個屬性成員,通過調(diào)用它的方法來include各種model(模型)、view(視圖)、database(數(shù)據(jù)庫對象)、helper(輔助函數(shù))等等。

  無論用不用動態(tài)加載,必須保證的是,文件分門別類的放好,文件按一定規(guī)則命名,這是一個健壯、高擴展、高易用的project必備的,寫起代碼來也方便。當(dāng)然加載文件的多少,占內(nèi)存的多少,各有不同,也是評判一個框架的若干標(biāo)準(zhǔn)。弄清楚加載方式,熟悉一個框架結(jié)構(gòu)不就是很容易的事了=_=...

?

  

?

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the current session ID in PHP? How to get the current session ID in PHP? Jul 13, 2025 am 03:02 AM

The method to get the current session ID in PHP is to use the session_id() function, but you must call session_start() to successfully obtain it. 1. Call session_start() to start the session; 2. Use session_id() to read the session ID and output a string similar to abc123def456ghi789; 3. If the return is empty, check whether session_start() is missing, whether the user accesses for the first time, or whether the session is destroyed; 4. The session ID can be used for logging, security verification and cross-request communication, but security needs to be paid attention to. Make sure that the session is correctly enabled and the ID can be obtained successfully.

PHP get substring from a string PHP get substring from a string Jul 13, 2025 am 02:59 AM

To extract substrings from PHP strings, you can use the substr() function, which is syntax substr(string$string,int$start,?int$length=null), and if the length is not specified, it will be intercepted to the end; when processing multi-byte characters such as Chinese, you should use the mb_substr() function to avoid garbled code; if you need to intercept the string according to a specific separator, you can use exploit() or combine strpos() and substr() to implement it, such as extracting file name extensions or domain names.

How do you perform unit testing for php code? How do you perform unit testing for php code? Jul 13, 2025 am 02:54 AM

UnittestinginPHPinvolvesverifyingindividualcodeunitslikefunctionsormethodstocatchbugsearlyandensurereliablerefactoring.1)SetupPHPUnitviaComposer,createatestdirectory,andconfigureautoloadandphpunit.xml.2)Writetestcasesfollowingthearrange-act-assertpat

How to split a string into an array in PHP How to split a string into an array in PHP Jul 13, 2025 am 02:59 AM

In PHP, the most common method is to split the string into an array using the exploit() function. This function divides the string into multiple parts through the specified delimiter and returns an array. The syntax is exploit(separator, string, limit), where separator is the separator, string is the original string, and limit is an optional parameter to control the maximum number of segments. For example $str="apple,banana,orange";$arr=explode(",",$str); The result is ["apple","bana

JavaScript Data Types: Primitive vs Reference JavaScript Data Types: Primitive vs Reference Jul 13, 2025 am 02:43 AM

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

Using std::chrono in C Using std::chrono in C Jul 15, 2025 am 01:30 AM

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

How to pass a session variable to another page in PHP? How to pass a session variable to another page in PHP? Jul 13, 2025 am 02:39 AM

In PHP, to pass a session variable to another page, the key is to start the session correctly and use the same $_SESSION key name. 1. Before using session variables for each page, it must be called session_start() and placed in the front of the script; 2. Set session variables such as $_SESSION['username']='JohnDoe' on the first page; 3. After calling session_start() on another page, access the variables through the same key name; 4. Make sure that session_start() is called on each page, avoid outputting content in advance, and check that the session storage path on the server is writable; 5. Use ses

PHP header location not working after include PHP header location not working after include Jul 13, 2025 am 02:08 AM

When encountering the problem that header('Location:...') does not work, the common reasons and solutions are as follows: 1. There is output in advance, causing the header to fail. The solution is to ensure that there is no output before the jump, including spaces, HTML or echo; 2. There is excess output or UTF-8 BOM characters in the include or require file. The file encoding should be checked and saved as "UTF-8 BOM-free"; 3. It is recommended to use ob_start() to turn on the output buffer before the jump, and cooperate with ob_end_flush() to delay the output; 4. After the jump, be sure to add exit to prevent subsequent code execution; 5. Make sure that the header() function call is before all outputs.

See all articles