根据官方文档依赖注入出现`ReflectionParameter::getClass()`的解决方法

shiroi

前提

根据官方文档来进行依赖注入,会出现Method ReflectionParameter::getClass() is deprecated的问题,该问题的产生是因为php8不支持getClass()写法,得用getType()的写法

解决方案

  • 在项目底下创建modify.php文件
  • 修改composer.json文件,新增post-install-cmdpost-update-cmd
    "scripts": {
    "post-install-cmd": [
      "php modify.php"
    ],
    "post-update-cmd": [
      "php modify.php"
    ]
    }
  • 编写modify.php文件
    <?php
    $AnnotationBasedAutowiring_file = 'vendor/php-di/php-di/src/Definition/Source/AnnotationBasedAutowiring.php';
    if (file_exists($AnnotationBasedAutowiring_file)) {
    // 读取文件内容
    $content = file_get_contents($AnnotationBasedAutowiring_file);
    // 检查文件是否成功读取
    if ($content !== false) {
        // 进行字符串替换
        $updatedContent = str_replace('$parameter->getClass()', '$parameter->getType()', $content);
        // 将替换后的内容写回文件
        file_put_contents($AnnotationBasedAutowiring_file, $updatedContent);
    }
    }

    这样的话,后续composer安装或者更新的同时去执行该文件

253 2 0
2个评论

lvhe

大佬厉害,修改,再composer 更新一下,就OK了

  • 暂无评论
莫等闲

为啥不直接提pr?或者自建仓库用自己的仓库,等官方修改了就用官方的。

  • 暂无评论

shiroi

380
积分
0
获赞数
0
粉丝数
2024-02-22 加入
×
🔝