我正在尝试使用新的 preloading feature自 PHP 7.4 起可用。
我运行 composer install --no-dev --optimize-autoloader
来生成项目中所有可用类的列表,并使用以下 preload.php
预加载它们的脚本:
$files = require 'vendor/composer/autoload_classmap.php';
foreach (array_unique($files) as $file) {
opcache_compile_file($file);
}
并在我的 opcache.ini
文件中配置这个预加载脚本:
opcache.preload=/path/to/preload.php
并重新启动 php-fpm
。现在 systemctl status php-fpm.service
报告以下警告:
PHP Warning: Can't preload unlinked class Brick\Money\Context\CashContext: Unknown type dependencies in ... on line 16
PHP Warning: Can't preload unlinked class Brick\Money\Context\AutoContext: Unknown type dependencies in ... on line 17
PHP Warning: Can't preload unlinked class Brick\Math\BigRational: Unknown type dependencies in ... on line 17
PHP Warning: Can't preload unlinked class Brick\Math\BigInteger: Unknown type dependencies in ... on line 20
PHP Warning: Can't preload unlinked class Brick\Math\BigDecimal: Unknown type dependencies in ... on line 15
“未知类型依赖项”是什么意思?如何预加载这些类?
注意:我是有问题的库的维护者 Brick\Math和 Brick\Money ,所以如果这些需要修改以使其可预加载,我洗耳恭听!
最佳答案
这意味着 PHP 在运行时无法在预加载文件中找到类。 This only happens when there might be methods that are incompatible :
Preloading: Relax known type restrictions
Check whether there is a parent/interface/trait method with the same name and only then require the type to be known. This reduces the number of cases where this triggers in practice a lot.
预加载的文件不使用 Composer 自动加载器,因此没有预加载的类将不存在。
Symfony 通过 creating a Preloader
class 修复了这个问题可以加载它的依赖项。这是 how to use it预加载 LoaderInterface
和 AnnotationClassLoder
:
<?php
// preload.php
$classes[] = 'Symfony\Component\Config\Loader\LoaderInterface';
$classes[] = 'Symfony\Component\Routing\Loader\AnnotationClassLoader';
Preloader::preload($classes);
在有更好的方法之前,您可以复制该类或预加载该库依赖的所有类。
如果您允许预加载失败是硬错误,请使用 answer from NikiC :
To avoid dependency issues, you can preload using
require
instead ofopcache_compile_file()
. This will handle circular dependencies fine, but will hard error if preloading fails (instead of just warning).
关于php - 预加载PHP脚本时 "Unknown type dependencies"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59520529/