PHP 8.2环境下Symfony/Composer出现弃用错误
问题
CentOS开发环境刚把PHP升级到8.2.10,尝试安装最新版mPDF时,执行composer require mpdf/mpdf出现一堆Symfony相关的弃用错误,同时系统提示不要用root身份运行Composer。当前Composer版本是2.1.3,本人擅长开发但运维不熟,求解决。
具体错误信息:
$ composer require mpdf/mpdf Deprecated: Return type of Symfony\Component\Console\Helper\HelperSet::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/share/php/Symfony3/Component/Console/Helper/HelperSet.php on line 104 Deprecation Notice: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /usr/share/php/JsonSchema5/Constraints/Constraint.php:48 Deprecation Notice: Return type of Symfony\Component\Process\Process::getIterator($flags = 0) should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/share/php/Symfony3/Component/Process/Process.php:567 Deprecation Notice: Return type of Composer\Repository\ArrayRepository::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/share/php/Composer/Repository/ArrayRepository.php:286 Deprecation Notice: Return type of Composer\Repository\ArrayRepository::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/share/php/Composer/Repository/ArrayRepository.php:286 Deprecation Notice: Return type of Composer\Repository\ArrayRepository::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/share/php/Composer/Repository/ArrayRepository.php:286 Deprecation Notice: Using ${var} in strings is deprecated, use {$var} instead in /usr/share/php/Composer/Autoload/AutoloadGenerator.php:799 Deprecation Notice: Using ${var} in strings is deprecated, use {$var} instead in /usr/share/php/Composer/Autoload/AutoloadGenerator.php:804 Do not run Composer as root/super user! See https://getcomposer.org/root for details
执行composer --version时也出现相同弃用错误:
$ composer --version Deprecated: Return type of Symfony\Component\Console\Helper\HelperSet::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/share/php/Symfony3/Component/Console/Helper/HelperSet.php on line 104 Deprecation Notice: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /usr/share/php/JsonSchema5/Constraints/Constraint.php:48 Deprecation Notice: Return type of Symfony\Component\Process\Process::getIterator($flags = 0) should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/share/php/Symfony3/Component/Process/Process.php:567 Deprecation Notice: Return type of Composer\Repository\ArrayRepository::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/share/php/Composer/Repository/ArrayRepository.php:286 Deprecation Notice: Return type of Composer\Repository\ArrayRepository::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/share/php/Composer/Repository/ArrayRepository.php:286 Deprecation Notice: Return type of Composer\Repository\ArrayRepository::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/share/php/Composer/Repository/ArrayRepository.php:286 Deprecation Notice: Using ${var} in strings is deprecated, use {$var} instead in /usr/share/php/Composer/Autoload/AutoloadGenerator.php:799 Deprecation Notice: Using ${var} in strings is deprecated, use {$var} instead in /usr/share/php/Composer/Autoload/AutoloadGenerator.php:804 Do not run Composer as root/super user! See https://getcomposer.org/root for details Continue as root/super user [yes]? Composer 2.1.3 2021-06-09 16:31:20
解决方案
升级Composer版本:你的Composer 2.1.3是2021年的旧版本,完全不兼容PHP 8.2。执行以下命令升级:
composer self-update如果当前是root用户,可加
--no-plugins参数避免冲突:composer self-update --no-plugins升级后,Composer会更新自身依赖的Symfony组件,那些弃用提示会自动消失。
不要以root身份运行Composer:
- 创建普通用户(如果没有):
useradd -m devuser passwd devuser - 给项目目录赋予devuser权限:
chown -R devuser:devuser /path/to/your/project - 切换到devuser执行Composer命令:
su - devuser cd /path/to/your/project composer require mpdf/mpdf
这样既符合安全规范,也能避免权限相关的潜在问题。
- 创建普通用户(如果没有):
验证修复效果:升级完成后,执行
composer --version,如果不再出现弃用提示,说明问题解决,再重新执行mPDF的安装命令即可。
内容的提问来源于stack exchange,提问作者abe1432181
相关产品推荐
相关产品推荐

