vlucas / phpdotenv

Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.
BSD 3-Clause "New" or "Revised" License
13.17k stars 629 forks source link

PHP 8.1 deprecation warning #504

Closed scottgruber closed 2 years ago

scottgruber commented 2 years ago

Hi,

I came across an error after updating to PHP 8.1 while loading a via CraftCMS site locally. The same error also happened using PHP8.0.

PHPWatch posted a new PHP8.1 attribute New #[ReturnTypeWillChange] attribute which may solve the issue.

Here is the error.

Deprecated: Return type of Dotenv\Environment\AbstractVariables::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /vendor/vlucas/phpdotenv/src/Environment/AbstractVariables.php on line 162

Deprecated: Return type of Dotenv\Environment\AbstractVariables::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in vendor/vlucas/phpdotenv/src/Environment/AbstractVariables.php on line 170

Deprecated: Return type of Dotenv\Environment\AbstractVariables::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /vendor/vlucas/phpdotenv/src/Environment/AbstractVariables.php on line 178

Deprecated: Return type of Dotenv\Environment\AbstractVariables::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /vendor/vlucas/phpdotenv/src/Environment/AbstractVariables.php on line 186

Before

/**
     * {@inheritdoc}
     */
    public function offsetExists($offset)
    {
        return $this->has($offset);
    }

After

/**
     * {@inheritdoc}
     */
    #[\ReturnTypeWillChange]
    public function offsetExists($offset)
    {
        return $this->has($offset);
    }

As a quick test, I took a look in AbstractVariables.php and added #[\ReturnTypeWillChange] before the public function declaration and the warning was suppressed. I read your contributing guidelines, and could try to make the changes and submit a PR, but I'm a junior PHP developer and might not code it correctly.

estebancastro commented 2 years ago

Same issue here, running CraftCMS locally with Laravel Homestead. I downgraded to PHP 8.0 and warning disappear.

GrahamCampbell commented 2 years ago

Ask them to upgrade to a newer version of phpdotenv. :)

scottgruber commented 2 years ago

~I downgraded too and that resolved fixed the issue for the moment, although I dropped down to php7.4. Too close to the holidays to worry about it.~

~valet use php@7.4~ ~composer global update~ ~valet restart~

EDIT FROM MAINTAINER: DO NOT DO THIS. Do not downgrade PHP to "fix" issues with this package - the real problem is that you mistakenly installed an old version of this package - a common mistake is writing 5.0 instead of ^5.0 - check your composer.json file.

GrahamCampbell commented 2 years ago

Fixed.

Tsdjimmy commented 2 years ago

~Install a compatible php version with brew install php@7.4~

~and use it by running~ ~Brew link php@7.4~

EDIT FROM MAINTAINER: DO NOT DO THIS. Do not downgrade PHP to "fix" issues with this package - the real problem is that you mistakenly installed an old version of this package - a common mistake is writing 5.0 instead of ^5.0 - check your composer.json file.

Tsdjimmy commented 2 years ago

Install a compatible php version with brew install php@7.4

and use it by running Brew link php@7.4

THE ABOVE IS FOR MAC USERS

GrahamCampbell commented 2 years ago

@Tsdjimmy that is not good advice. You need to instead upgrade to the latest phpdotenv version in the major series you are using. The latest 2.x, 3.x, 4.x and 5.x releases all run on PHP 8.1 without this error.

selenearzola commented 2 years ago

scottgruber thanks!!

GrahamCampbell commented 2 years ago

@selenearzola I re-iterate: it is not a good idea to use out of date software. The latest version of each 2.x, 3.x, 4.x and 5.x series package is tested on, and known to work with the latest version of PHP. Do not downgrade PHP to "fix" issues with this package - the real problem is that you mistakenly installed an old version of this package - a common mistake is writing 5.0 instead of ^5.0 - check your composer.json file.