EasyPost/easypost-php

PHP v8.1 incompatible

Closed this issue · 5 comments

When called under PHP v8.1 there are several functions in EasyPostObject.php (ArrayAccess and Iterator methods) which need to be updated or add the comment #[\ReturnTypeWillChange] to prevent deprecation notice

Can you provide a few specific examples (line numbers) that we could take a look at?

(The quick fix) Beginning at line #226 through #308 insert this line between the comment and the (9) function statements for each function

    #[\ReturnTypeWillChange]

whereby

    /**
     * ArrayAccess methods
     *
     * @param string $k
     * @param mixed $v
     */
    public function offsetSet($k, $v)
    {
        $this->$k = $v;
    }

becomes

    /**
     * ArrayAccess methods
     *
     * @param string $k
     * @param mixed $v
     */
    #[\ReturnTypeWillChange]
    public function offsetSet($k, $v)
    {
        $this->$k = $v;
    }

Note that using #[\ReturnTypeWillChange] would only be a temporary hack to hide deprecation notices and will break with PHP 9.0 because return types will be strictly enforced.

The proper fix for future PHP versions is to add return type declarations.

https://php.watch/versions/8.1/internal-method-return-types

@dleffler thanks for submitting this ticket and the additional info! I did some initial work via #129 but return type declarations weren't added until PHP 7 and we still support PHP 5.3. We are going to drop support for PHP 5 shortly in favor of modernizing the library to use new features like this and to prepare for PHP 9 and others. I'll return to this PR shortly once this is unblocked.

I've determined we'll move forward with ignoring the deprecation warnings for now as we need the mixed return type which wasn't introduced until PHP 8 - therefore adding the return types isn't possible as we plan to continue supporting PHP 7 for a bit longer. Once PHP 9 comes out, we can drop support for PHP 7 and add these return types in properly.