关于 Laravel 项目中自动载入对性能的影响
上周在一次研究laravel的时候,无意中看到了/vendor/composer/ClassLoader.php的这一段代码如下:
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
includeFile($file);
return true;
}
}
/**
* Finds the path to the file where the class is defined.
*
* @param string $class The name of the class
*
* @return string|false The path if found, false otherwise
*/
public function findFile($class)
{
// work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731
if ('\\' == $class[0]) {
$class = substr($class, 1);
}
// class map lookup
if (isset($this->classMap[$class])) {
return $this->classMap[$class];
}
if ($this->classMapAuthoritative) {
return false;
}
$file = $this->findFileWithExtension($class, '.php');
// Search for Hack files if we are running on HHVM
if ($file === null && defined('HHVM_VERSION')) {
$file = $this->findFileWithExtension($class, '.hh');
}
if ($file === null) {
// Remember that this class does not exist.
return $this->classMap[$class] = false;
}
return $file;
}
我认为这就是laravel的自动载入的CLASS的关键, 我稍微调试了一下这段代码发现这段代码在整个项目执行的过程中经常被调用(实际上接替了你写INCLUDE), 我想知道这段代码对性能的影响大不大?
推荐文章: