PHP實現懶載入的方法,php實現載入_PHP教程

來源:互聯網
上載者:User

PHP實現懶載入的方法,php實現載入


本文執行個體講述了PHP實現懶載入的方法。分享給大家供大家參考。具體分析如下:

尋常php的載入是通過include(),require()等方法來載入外部檔案,之後再通過執行個體調用方法或直接調用靜態方法,而這樣子寫引入語句實在很麻煩,有的架構會將特定路徑的檔案全部引入,直接執行個體化就能使用,但這樣一來有的類包不一定用到,寫的類包越多的時候,載入的東西就不少了,影響程式的效能。

通過PHP的反射類 ReflectionClass 可以直接獲得對應類的一個反射類:

test.php檔案如下:

<?php class test{   public function showName(){     var_dump(__CLASS__);   } }?>

index.php檔案如下:

<?phpvar_dump(get_included_files()); $rf = new ReflectionClass('test');var_dump(get_included_files());$testObj = $rf->newInstance();$testObj->showName();function __autoload($classname){  $classpath = './' . $classname . '.php';  if (file_exists($classpath)) {    require_once($classpath);  }else {    echo 'class file'.$classpath.'not found!';  }}?>//array// 0 => string 'D:\code\www\test\index.php'(length=26)//array// 0 => string 'D:\code\www\test\index.php'(length=26)// 1 => string 'D:\code\www\text\test.php'(length=25)//string 'test' (length=4)

執行個體化一個 ReflectionClass,並傳類名進去,就會得到一個對應類的反射類。用執行個體調用 newInstance就會得到反射類的執行個體,這樣就完成了執行個體化。

通過 get_included_files() 方法,我們可以看到當前頁面引入的檔案。在執行個體化反射類前,只有index.php檔案,執行個體化反射類後,自動引入了一個test.php檔案,那麼看下上面那段代碼,發現有個__autoload()名字的魔術方法,這方法就定義了自動負載檔案,而ReflectionClass在當前頁面找不到類時,就會調用__autoload()去載入類。這就是自動載入的過程。

想知道__autoload()方法有沒有開啟,可以通過PHP的標準庫SPL中的方法來查看:

var_dump(spl_autoload_functions());spl_autoload_register('newAutoload');var_dump(spl_autoload_functions());$testObj1 = getInstance('test');$testObj2 = getInstance('test');$testObj3 = getInstance('test');function getInstance($class, $returnInstance = false){  $rf = new ReflectionClass($class);  if ($returnInstance)     return $rf->newInstance();}function newAutoload($classname){  $classpath = './' . $classname . '.php';  if (file_exists($classpath)) {    var_dump('require success');    require_once($classpath);  } else {    echo 'class file ' . $classpath . ' not found!';  }}//array// 0 => string '__autoload' (length=10)//array// 0 => string 'newAutoload' (length=11)//string 'require success' (length=15)

sql_autoload_functions() 方法是用來查看當前自動載入的方法,當前有個__autoload魔術方法,所以返回了函數名,若沒定義自動載入方法的話,返回的是false,而 spl_autoload_register() 方法是通過方法名將一個方法註冊到自動載入方法,這裡用newAutoload方法來替換__autoload方法。

newAutoload方法中,每執行成功一次,列印一句'require success',這裡只列印了一次,說明了雖然執行個體了3次ReflectionClass('test'),但因為test類已經載入過一次,就不會再執行自動載入的方法。通過getInstance()這種載入類的方法,比以前的include()之類的方便多了,只需要載入這個寫了getInstance()方法的檔案就可以了。

重寫的自動載入方法可以根據需要,通過判斷類的名字,定義不同的檔案路徑。getInstance可以用靜態變數儲存執行個體,這也是使用到了設計模式中的單例模式。

希望本文所述對大家的php程式設計有所協助。

http://www.bkjia.com/PHPjc/963826.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/963826.htmlTechArticlePHP實現懶載入的方法,php實現載入 本文執行個體講述了PHP實現懶載入的方法。分享給大家供大家參考。具體分析如下: 尋常php的載入是通過i...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.