php 8.1 - 旧脚本中弃用的返回类型

标签 php deprecated arrayaccess php-8.1

<分区>

尝试更新到 php 8.1 并注意到这个已弃用的通知出现在我想处理的错误日志中。

[14-Feb-2022 14:48:25 UTC] PHP 已弃用:TLDExtractResult::offsetExists($offset) 的返回类型应与 ArrayAccess::offsetExists(mixed $offset) 兼容:bool,或者应该使用#[\ReturnTypeWillChange] 属性暂时抑制第 299 行/home/example/public_html/assets/tldextract/tldextract.php 中的通知

我能够抑制警告,但实际上我想更新脚本以便将来不会出现问题。

//public function offsetExists($offset) {
//  return array_key_exists($offset, $this->fields);
//}

#[\ReturnTypeWillChange]
public function offsetExists(mixed $offset) {
    return array_key_exists($offset, $this->fields);
}

这是有问题的代码部分:

class TLDExtractResult implements ArrayAccess {
    private $fields;

    public function __construct($subdomain, $domain, $tld) {
        $this->fields = array(
            'subdomain' => $subdomain,
            'domain'    => $domain,
            'tld'       => $tld,
        );
    }

    public function __get($name) {
        if ( array_key_exists($name, $this->fields) ) {
            return $this->fields[$name];
        }
        throw new OutOfRangeException(sprintf('Unknown field "%s"', $name));
    }

    public function __isset($name) {
        return array_key_exists($name, $this->fields);
    }

    public function __set($name, $value) {
        throw new LogicException('Can\'t modify an immutable object.');
    }

    public function __toString() {
        return sprintf('%s(subdomain=\'%s\', domain=\'%s\', tld=\'%s\')', __CLASS__, $this->subdomain, $this->domain, $this->tld);
    }

    //public function offsetExists($offset) {
    //  return array_key_exists($offset, $this->fields);
    //}
    
    #[\ReturnTypeWillChange]
    public function offsetExists(mixed $offset) {
        return array_key_exists($offset, $this->fields);
    }

    //public function offsetGet($offset) {
    //  return $this->__get($offset);
    //}

    #[\ReturnTypeWillChange]
    public function offsetGet(mixed $offset) {
        return $this->__get($offset);
    }

    //public function offsetSet($offset, $value) {
    //  throw new LogicException(sprintf('Can\'t modify an immutable object. You tried to set "%s".', $offset));
    //}

    #[\ReturnTypeWillChange]
    public function offsetSet(mixed $offset, $value) {
        throw new LogicException(sprintf('Can\'t modify an immutable object. You tried to set "%s".', $offset));
    }

    //public function offsetUnset($offset) {
    //  throw new LogicException(sprintf('Can\'t modify an immutable object. You tried to unset "%s".', $offset));
    //}

    #[\ReturnTypeWillChange]
    public function offsetUnset(mixed $offset) {
        throw new LogicException(sprintf('Can\'t modify an immutable object. You tried to unset "%s".', $offset));
    }

    /**
     * Get the domain name components as a native PHP array.
     * The returned array will contain these keys: 'subdomain', 'domain' and 'tld'.
     *
     * @return array
     */
    public function toArray() {
        return $this->fields;
    }
}

php 8.1 到底有什么不喜欢的地方?这比我通常使用 php 所做的要高一点,并且不确定这个问题。听起来需要指定特定的返回类型?

最佳答案

是的,您可以指定与为 ArrayAccess 接口(interface)的各种方法指定的返回类型相匹配的返回类型,如图所示 in the manual .例如,对于您问题中弃用消息中的特定方法,就像这样:

public function offsetExists(mixed $offset): bool {
    return array_key_exists($offset, $this->fields);
}

关于php 8.1 - 旧脚本中弃用的返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71133132/

相关文章:

.net-4.5 - .Net 4.5 中是否已弃用 RecordSet 对象?

php - PHP 中的 ArrayAccess——通过引用分配给偏移量

php - 模拟/ stub 一个在 PHPUnit 中实现 arrayaccess 的类的对象

php/jquery 搜索框

java - java中@deprecated的逆向是什么

PHP不显示任何错误

angularjs - ngclick 已弃用。流行的替代方案或解决方案?

php - 在实现 ArrayAccess 和 Iterator 的对象上使用 foreach

PHP - Codeigniter 在插入数据库时​​删除破折号和括号

javascript - 来自文本文件的 AJAX 请求