PHP PSR4自動載入代碼賞析

來源:互聯網
上載者:User

標籤:入口   自動   keyword   hvm   ...   lookup   line   dcl   class   

第一部分是引入自動載入設定檔

1.入口檔案:autoload.php
裡面沒什麼東西,就是匯入ComposerAutoloader主題檔案,一般由一個複雜的名字,不過不用擔心就是機器隨機產生的一個碼而已,就是普通的一個類,名字比較長了。

require_once __DIR__ . ‘/composer/autoload_real.php‘;return ComposerAutoloaderInitd0a5721608b46fc86f3b980fb0cea37d::getLoader();

2.自動載入主題檔案:ComposerAutoloaderInitd0a5721608b46fc86f3b980fb0cea37d
getLoader(){.....}
這個就是獲得自動載入的主方法,這個有點像代加工廠、裡面其實只是組裝了下,實際的組件在別的類(後面會提到的ClassLoader類),接下來說說getLoader方法做了哪些代加工

  • 把實際幹活的小工招進廠->
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
  • 判斷了下PHP版本 如果版本大於5.6 就使用閉包綁定的方式去進行綁定自動載入的配置、需要注意的是,這些內容其實都是composer dump-autoload命令產生的 所以修改了composer.json後一定要執行下該命令。下面的程式碼片段就是給各種psr規則都設定進私人變數,閉包居然還可以這麼直接搞好犀利的趕腳
return \Closure::bind(function () use ($loader) { $loader->prefixLengthsPsr4 = ComposerStaticInitd0a5721608b46fc86f3b980fb0cea37d::$prefixLengthsPsr4; $loader->prefixDirsPsr4 = ComposerStaticInitd0a5721608b46fc86f3b980fb0cea37d::$prefixDirsPsr4; $loader->prefixesPsr0 = ComposerStaticInitd0a5721608b46fc86f3b980fb0cea37d::$prefixesPsr0;}, null, ClassLoader::class);
  • 如果版本小於5.6就使用原始點的方法,通過setxxx來一個個進行設定自動載入的配置,和上面實現的功能其實一樣的,但是代碼量就大很多了
$map = require __DIR__ . ‘/autoload_namespaces.php‘;foreach ($map as $namespace => $path) { $loader->set($namespace, $path);}$map = require __DIR__ . ‘/autoload_psr4.php‘;foreach ($map as $namespace => $path) { $loader->setPsr4($namespace, $path);}$classMap = require __DIR__ . ‘/autoload_classmap.php‘;if ($classMap) { $loader->addClassMap($classMap);}
  1. 最後把loader類註冊一下
spl_autoload_register(array($this, ‘loadClass‘), true, $prepend);
第二部分是如何通過類名找到該檔案並引入
  1. 入口方法:ClassLoader.php中的loadClass(),尋找順序是 先classMap裡找 再PSR4 PSR0 找
// class map lookupif (isset($this->classMap[$class])) { return $this->classMap[$class];}if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { return false;}if (null !== $this->apcuPrefix) { $file = apcu_fetch($this->apcuPrefix.$class, $hit); if ($hit) { return $file; }}$file = $this->findFileWithExtension($class, ‘.php‘);// Search for Hack files if we are running on HHVMif (false === $file && defined(‘HHVM_VERSION‘)) { $file = $this->findFileWithExtension($class, ‘.hh‘);}if (null !== $this->apcuPrefix) { apcu_add($this->apcuPrefix.$class, $file);}if (false === $file) { // Remember that this class does not exist. $this->missingClasses[$class] = true;}return $file;

PHP PSR4自動載入代碼賞析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.