使用 Composer,有些库即使我没有使用,库文件也会被加载进来。

WBOY
Release: 2016-06-06 20:31:46
Original
1154 people have browsed it

我发现安装的这两个库:
wapmorgan/unified-archive
qiniu/php-sdk
不管我在项目中有没有用到,都会把库文件加载进来,而且是在加载完composer的autoload后直接就加载了。
Composer不是只在用到类的时候才会加载对应的文件吗?

回复内容:

我发现安装的这两个库:
wapmorgan/unified-archive
qiniu/php-sdk
不管我在项目中有没有用到,都会把库文件加载进来,而且是在加载完composer的autoload后直接就加载了。
Composer不是只在用到类的时候才会加载对应的文件吗?

不一定,有些类库是在autoload文件中总是加载的,可以看源码vendor/composer/autoload_real.php

<code>        $includeFiles = require __DIR__ . '/autoload_files.php';
        foreach ($includeFiles as $file) {
            // 读取autoload_files.php文件内容,每次都会加载
            composerRequireef7b9e9cb1f47f35cfc22e88ff512c1d($file);
        }

        return $loader;
    }
}

function composerRequireef7b9e9cb1f47f35cfc22e88ff512c1d($file)
{
    // 直接require文件
    require $file;
}
</code>
Copy after login

这里加载了autoload_files.php,比如我用的medoo,这里就自动加载了

<code><?php // autoload_files.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    $vendorDir . '/catfan/medoo/medoo.php',
);
</code></code>
Copy after login
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template