~/codeDir/phpCode/autoloads # composer dump Do not run Composer as root/super user! See https://getcomposer.org/root for details Generated autoload files containing 0 classes ~/codeDir/phpCode/autoloads #
\$t = new Test(); echo \$t->func1(); EOF ~/codeDir/phpCode/autoloads #
然后执行命令:
1 2 3
~/codeDir/phpCode/autoloads # composer dump Do not run Composer as root/super user! See https://getcomposer.org/root for details Generated autoload files containing 1 classes
~/codeDir/phpCode/autoloads # composer dump Do not run Composer as root/super user! See https://getcomposer.org/root for details Generated autoload files containing 1 classes ~/codeDir/phpCode/autoloads #
~/codeDir/phpCode/autoloads # php test.php in func1.in Test->func1in Test->func1~/codeDir/phpCode/autoloads #
调用成功。
全部使用classmap自动加载
执行命令:
1 2 3 4
~/codeDir/phpCode/autoloads # composer dumpautoload -o Do not run Composer as root/super user! See https://getcomposer.org/root for details Generated optimized autoload files containing 2 classes ~/codeDir/phpCode/autoloads #
~/codeDir/phpCode/autoloads # composer dumpautoload --help Options: -o, --optimize Optimizes PSR0 and PSR4 packages to be loaded with classmaps too, good for production.
也就是说,会自动把我们配置好的psr-0以及psr-4加载方式转化为classmap的方式加载。
如果我们要取消这种优化,那么我们可以执行命令:
1 2 3 4
~/codeDir/phpCode/autoloads # composer dumpautoload Do not run Composer as root/super user! See https://getcomposer.org/root for details Generated autoload files containing 1 classes ~/codeDir/phpCode/autoloads #
~/codeDir/phpCode/autoloads # composer dumpautoload -a Do not run Composer as root/super user! See https://getcomposer.org/root for details Generated optimized autoload files (authoritative) containing 2 classes ~/codeDir/phpCode/autoloads #
然后,我们看看findFile的代码:
1 2 3 4 5 6 7 8 9 10
publicfunctionfindFile($class) { // class map lookup if (isset($this->classMap[$class])) { return$this->classMap[$class]; } if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { returnfalse; } // 省略其他的代码
此时$this->classMapAuthoritative的值会变成true。所以,一旦:
1 2 3
if (isset($this->classMap[$class])) { return$this->classMap[$class]; }