php - 预加载PHP脚本时 "Unknown type dependencies"是什么意思?

标签 php preloading php-7.4

我正在尝试使用新的 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\MathBrick\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预加载 LoaderInterfaceAnnotationClassLoder:

<?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 of opcache_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/

相关文章:

php - 我正在访问日期为 : how to assign a value when a particular date is not present? 的记录

php - Doctrine2 - "class"不是有效的实体或映射的父类(super class)

jquery - 如何预加载图像以便稍后使用 jQuery 将其设置为背景图像?

javascript - Chrome 扩展程序 : how to change JS url before he starts loading

php - html与php的编码

php - appcfg.py : error: Error parsing app\app. yaml:无法将值 'php55' 分配给属性 'runtime'

php - php 7.4 写入变量中的 Twig 问题

php - 在 ubuntu 上启动 php7.4-fpm 时出现问题

php - 如何从 PHP 7.4+ 中的对象中删除属性