Working on updating an old app to PHP 8. I got everything running so far (at least according to the test suite) in 8.0, but when I upgrade to 8.1 I'm getting this error:

Fatal error: During inheritance of ArrayObject: Uncaught ErrorException: Return type of PropertySet::offsetGet($key) should either be compatible with ArrayObject::offsetGet(mixed $key): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

This is the method that triggers it:

class PropertySet extends ArrayObject {
    // ... other methods
    function offsetGet($key) {
        $argc = func_num_args();
        if ($this->offsetExists($key)) {
            return parent::offsetGet($key);
        }
        else if ($argc == 2) {
            return func_get_arg(1);
        }
        else {
            throw new FException(500, "Key '$key' does not exist");
       }
    }
}

Since my initial google search didn't return anything with a nice, clear, actionable solution; I figured I'd post this here in case anyone has run into it (and if nothing else, once I solve it, it may help anyone who runs into this 🙂 ).

    Okay, that was easier that I was led to believe from my initial search results. I just added the return type to the method declarations:

    function offsetGet($key): mixed {
    
      NogDog changed the title to SOLVED: 8.1 warning re return type of inherited method.

        You could document the second parameter:

            function offsetGet($key, $default = null): mixed {
                if ($this->offsetExists($key)) {
                    return parent::offsetGet($key);
                }
                else if (func_num_args() == 2) {
                    return $default;
                }
                else {
                    throw new FException(500, "Key '$key' does not exist");
               }
            }
        

        (I expect you want to allow for a null default, hence func_num_args() instead of isset($default).) Both to make it visible in the method signature and to give it a name when using named arguments.

        Weedpacket Good point...I didn't even look all that closely at it, as I was just going through various deprecated warnings and stuff; but decided I could even get reid of the func_num_args() stuff, too. 🙂

        function offsetGet($key, $default = null): mixed {
          if ($this->offsetExists($key)) {
            return parent::offsetGet($key);
          }
          else if (!is_null($default)) {
            return $default;
          }
          else {
            throw new FException(500, "Key '$key' does not exist");
          }
        }
        
          22 days later

          You can get rid of the else. Your either returning one of the the if's or your throwing the exception. Makes it slightly cleaner.

          benanamen

          True. To be honest, though, I suspect I won't look at that again based on where the project is now. (At this point my pull request has some ridiculous number of changed files. 🤦‍♂️ )

            Write a Reply...