简体   繁体   English

PHP 8.1 中特定 class 的对象数组是否有类型提示?

[英]Is there a type hint for an array of objects of a specific class in PHP 8.1?

Is there a type hint in PHP 8.1 which allows using ExampleClass[] for declaring it is an array of objects of the class ExampleClass ? PHP 8.1中是否有类型提示,它允许使用ExampleClass[]声明它是 class ExampleClass的对象数组


In my specific case, ExampleClass is called Task在我的具体情况下, ExampleClass被称为Task

What I want but does not work:我想要但不起作用:

private Task[] $tasks;

My PHPStorm IDE tells me that Plural types are allowed only in doc types我的 PHPStorm IDE 告诉我复数类型只允许在文档类型中
- speaking that only in PHPDoc using Tasks[] would be totally fine. - 说只有在PHPDoc中使用Tasks[]就可以了。 But I want to use plain PHP .但我想使用普通的 PHP

The error is: PHP Parse error: syntax error, unexpected token "[", expecting variable错误是: PHP Parse error: syntax error, unexpected token "[", expecting variable


If I would only need one object Task and not an array of objects Task[] it would work with:如果我只需要一个 object Task而不是一个对象数组Task[]它可以使用:

private Task $task;

This is my current, working workaround:这是我目前的工作解决方法:

private array $tasks;

The short answer is no.最简洁的答案是不。

My understanding is that enforcing such a thing has terrible performance problems.我的理解是,执行这样的事情会带来可怕的性能问题。 Some people want to go all the way and have full blown generics, others just want type-safe collections.有些人想一直到 go 并拥有完整的 generics,其他人只想要类型安全的 collections。 Some people worry that implementing the latter first might hinder the development of the former in the future.有人担心,实施后者可能会阻碍前者未来的发展。 So we're at a standstill.所以我们处于停滞状态。

There's the occasional discussion in the community such as this: https://externals.io/message/108175社区里偶尔会有这样的讨论: https://externals.io/message/108175

While there is not a type hint available for such compound types, there is a workaround that enforces type.虽然没有可用于此类复合类型的类型提示,但有一种强制类型的解决方法。 Create a subclass from ArrayObject and validate all values added to the ArrayObject.从 ArrayObject 创建一个子类并验证添加到 ArrayObject 的所有值。 Then you can type hint your new subclass.然后你可以输入提示你的新子类。

<?php

class Collection extends ArrayObject
{
    public function __construct(array $items = [])
    {
        foreach ($items as $item) {
            $this->validate($item)
        }
    }

    public function append($value): void
    {
        $this->validate($value);
        parent::append($value);
    }

    public function offsetSet($key, $value): void
    {
        $this->validate($value);
        parent::offsetSet($key, $value);
    }

    protected function validate($value): void
    {
        if (!$value instanceof Task) {
            throw new InvalidArgumentException(
                'Not an instance of Task'
            );
        }
    }
}

Now you can return objects with a type hint of Collection and it will behave exactly as an array of Task objects.现在您可以返回带有Collection类型提示的对象,它的行为将与 Task 对象数组完全一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM