phplaravelcomposer-phplaravel-6laravel-artisan

During inheritance of ArrayAccess: Uncaught ErrorException: Return type of Illuminate\Support\Collection::offsetExists($key) should either be


I have an old laravel app (v6) that is still running on PHP 7.4

Laravel and PHP versions both out of support, I know! That upgrade is a task for another day.

But strange error started when running composer update.

/usr/bin/php7.2 /usr/bin/composer upgrade


During inheritance of ArrayAccess: Uncaught ErrorException: Return type of Illuminate\Support\Collection::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool,   
  or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/core/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1277            
  Stack trace:                                                                                                                                                                                                    
  #0 /var/www/html/core/vendor/laravel/framework/src/Illuminate/Support/Collection.php(11): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError()                                        
  #1 /var/www/html/core/vendor/composer/ClassLoader.php(576): include('...')                                                                                                                       
  #2 /var/www/html/core/vendor/composer/ClassLoader.php(427): Composer\Autoload\{closure}()                                                                                                        
  #3 /var/www/html/core/vendor/laravel/framework/src/Illuminate/Support/helpers.php(109): Composer\Autoload\ClassLoader->loadClass()                                                               
  #4 /var/www/html/core/vendor/laravel/framework/src/Illuminate/Foundation/PackageManifest.php(89): collect()                                                                                      
  #5 /var/www/html/core/vendor/laravel/framework/src/Illuminate/Foundation/PackageManifest.php(78): Illuminate\Foundation\PackageManifest->config()                                                
  #6 /var/www/html/core/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/RegisterFacades.php(26): Illuminate\Foundation\PackageManifest->aliases()                                     
  #7 /var/www/html/core/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(219): Illuminate\Foundation\Bootstrap\RegisterFacades->bootstrap()                                      
  #8 /var/www/html/core/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(320): Illuminate\Foundation\Application->bootstrapWith()                                             
  #9 /var/www/html/core/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(129): Illuminate\Foundation\Console\Kernel->bootstrap()                                              
  #10 /var/www/html/core/artisan(34): Illuminate\Foundation\Console\Kernel->handle()                                                                                                               
  #11 {main}      

This error seems to be a mismatch between php version and code format, however the error doesn't make sense.

https://www.php.net/manual/en/class.returntypewillchange.php The ReturnTypeWillChange attribute (PHP 8 >= 8.1.0)

This attribute suggested in the error message didn't even exist until php8.1 yet I and specifying php7.4

So logically, the php version I am doesn't even know this attribute so could not suggest this solution in the error message.

/usr/bin/php7.4 -v
PHP 7.4.33 (cli) (built: Dec 24 2024 07:11:50) ( NTS )

Also odd is that running Artisan command things work fine on 7.4 and error on 8.1

/usr/bin/php7.4 artisan --version
Laravel Framework 6.20.45

/usr/bin/php8.1 artisan --version
PHP Fatal error:  During inheritance of ArrayAccess: Uncaught ErrorException: Return type of Illuminate\Support\Collection::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $of

This confirms that the error is running php7.4 compatible version of Illuminate\Support\Collection on php8.1

But I am running the command in php7.4, so this shouldn't happen!

Have regularly run composer by passing it to a specific php version which always works great, but something seems to be switching php version randomly mid process.

Have tried downgrade to composer2.2 but still same error.


Solution

  • You have multiple versions of PHP installed, and I bet the one on your path as just php will be 8.1 or later. You can confirm this by running which php, or php -v.

    Based on the stack trace, this is happening because of the package:discover script, which is registered in the composer file as php artisan package:discover.

    Open your composer.json file and try changing @php to /usr/bin/php7.4 and run it again.

    Laravel 6 composer file for reference: https://github.com/laravel/laravel/blob/6.x/composer.json