You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maatwebsite Excel安装报错:MemoryCache与PsrExt\SimpleCache兼容问题

Laravel项目PSR缓存接口兼容性异常问题

项目Composer依赖配置

"require": {
    "php": "^8.1",
    "ext-intl": "*",
    "ext-openssl": "*",
    "ext-zip": "*",
    "arielmejiadev/larapex-charts": "^5.2",
    "barryvdh/laravel-dompdf": "^2.0",
    "diglactic/laravel-breadcrumbs": "^7.1",
    "doctrine/dbal": "^3.1",
    "guzzlehttp/guzzle": "^7.0.1",
    "intervention/validation": "^3.2",
    "jorijn/laravel-security-checker": "^2.3",
    "kwn/number-to-words": "^2.4",
    "laravel/framework": "^v9.0",
    "laravel/tinker": "^2.5",
    "league/oauth2-facebook": "^2.2",
    "maatwebsite/excel": "^3.1",
    "madorin/matex": "^1.0",
    "pbmedia/laravel-ffmpeg": "^8.1",
    "phpoffice/phpspreadsheet": "^1.22",
    "revolution/laravel-google-sheets": "^6.0",
    "spatie/laravel-activitylog": "^4.5",
    "spatie/laravel-sql-commenter": "^1.2"
},

问题现象

该项目在本地及两台服务器上运行正常,但在某一台服务器安装时触发致命错误:

PHP Fatal error:  Declaration of Maatwebsite\Excel\Cache\MemoryCache::get(string $key, mixed $default = null): mixed must be compatible with PsrExt\SimpleCache\CacheInterface::get($key, $default = <default>) in /var/www/html/livepartners_v3.com/vendor/maatwebsite/excel/src/Cache/MemoryCache.php on line 62
  • 手动指定安装"psr/simple-cache": "2.0"可解决该问题,但其他10台设备自动安装"psr/simple-cache": "3.0"却能正常运行
  • 执行composer update --no-script可暂时恢复运行,但后续执行dump-autoload时仍会触发相同错误

接口与实现方法签名对比

对比Psr\SimpleCache\CacheInterface的get方法与MemoryCache的实现,两者签名完全一致:

Psr\SimpleCache\CacheInterface::get

<?php

namespace Psr\SimpleCache;

interface CacheInterface
{
    /**
     * Fetches a value from the cache.
     *
     * @param string $key     The unique key of this item in the cache.
     * @param mixed  $default Default value to return if the key does not exist.
     *
     * @return mixed The value of the item from the cache, or $default in case of cache miss.
     *
     * @throws \Psr\SimpleCache\InvalidArgumentException
     *   MUST be thrown if the $key string is not a legal value.
     */
    public function get(string $key, mixed $default = null): mixed;

Maatwebsite\Excel\Cache\MemoryCache::get

namespace Maatwebsite\Excel\Cache;

use Psr\SimpleCache\CacheInterface;

class MemoryCache implements CacheInterface
{
    public function get(string $key, mixed $default = null): mixed
    {
        if ($this->has($key)) {
            return $this->cache[$key];
        }

        return $default;
    }
}

已尝试的排查操作

  • 更新PHP至8.1.21版本
  • 升级Composer到最新版本
  • 清理Composer缓存、删除vendor目录后重新安装依赖
  • 测试不同PHP版本(均为8.1分支)

核心疑问

为何仅该服务器会解析出无类型声明的PsrExt\SimpleCache\CacheInterface::get方法,其他服务器均无此问题?


内容的提问来源于stack exchange,提问作者Tapacko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 15:47:41