PHP 7 中的类型提示 - 对象数组 [英] Type hinting in PHP 7 - array of objects

查看:24
本文介绍了PHP 7 中的类型提示 - 对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我错过了一些东西,但是有没有任何选项可以定义该函数应该有参数或返回例如用户对象数组?

Maybe I missed something but is there any option to define that function should have argument or return for example array of User objects?

考虑以下代码:

<?php

class User
{
    protected $name;

    protected $age;

    /**
     * User constructor.
     *
     * @param $name
     */
    public function __construct(string $name, int $age)
    {
        $this->name = $name;
        $this->age = $age;
    }

    /**
     * @return mixed
     */
    public function getName() : string
    {
        return $this->name;
    }

    public function getAge() : int
    {
        return $this->age;
    }
}

function findUserByAge(int $age, array $users) : array
{
    $result = [];
    foreach ($users as $user) {
        if ($user->getAge() == $age) {
            if ($user->getName() == 'John') {
                // complicated code here
                $result[] = $user->getName(); // bug
            } else {
                $result[] = $user;
            }
        }
    }

    return $result;
}

$users = [
    new User('John', 15),
    new User('Daniel', 25),
    new User('Michael', 15),
];

$matches = findUserByAge(15, $users);

foreach ($matches as $user) {
    echo $user->getName() . ' '.$user->getAge() . "\n";
}

PHP7 中是否有任何选项告诉函数 findUserByAge 应该返回用户数组?我希望当添加类型提示时它应该是可能的,但我还没有找到任何关于对象数组类型提示的信息,所以它可能不包含在 PHP 7 中.如果它不包含,你有任何线索为什么它是添加类型提示时不包括在内?

Is there any option in PHP7 to tell function findUserByAge should return array of users? I would expect that when type hinting was added it should be possible but I haven't found any info for type hinting for array of objects so probably it's not included in PHP 7. If it's not included, do you have Any clue why it was not included when type hinting was added?

推荐答案

不包括在内.

如果没有包含,你知道为什么在添加类型提示时没有包含它吗?

If it's not included, do you have Any clue why it was not included when type hinting was added?

使用当前的数组实现,它需要在运行时检查所有数组元素,因为数组本身不包含类型信息.

With the current array implementation, it would require checking all array elements at runtime, because the array itself contains no type information.

它实际上已经被提议用于 PHP 5.6 但被拒绝:RFC "arrayof" - 有趣的是不是因为性能问题结果证明这是微不足道的,但因为在它应该如何实施方面没有达成一致.也有人反对说它没有标量类型提示是不完整的.如果您对整个讨论感兴趣,请阅读在邮件列表存档中.

It has actually already been proposed for PHP 5.6 but rejected: RFC "arrayof" - interestingly not because of performance issues which turned out to be neglible, but because there was no agreement in how exactly it should be implemented. There was also the objection that it is incomplete without scalar type hints. If you are interested in the whole discussion, read it in the mailing list archive.

恕我直言,数组类型提示与类型数组一起提供最大的好处,我很想看到它们被实现.

IMHO array type hints would provide most benefit together with typed arrays, and I'd love to see them implemented.

所以也许是时候制定新的 RFC 并重新开始讨论了.

So maybe it's about time for a new RFC and to reopen this discussion.

您可以输入提示可变参数,从而将签名写为

you can type hint variadic arguments and thus write the signature as

function findUserByAge(int $age, User ...$users) : array

用法:

findUserByAge(15, ...$userInput);

在这个调用中,参数 $userInput 将被解包"到单个变量中,并在方法本身打包"回一个数组 $users.每个项目都被验证为 User 类型.$userInput 也可以是一个迭代器,它会被转换成一个数组.

In this call, the argument $userInput will be "unpacked" into single variables, and in the method itself "packed" back into an array $users. Each item is validated to be of type User. $userInput can also be an iterator, it will be converted to an array.

遗憾的是,返回类型没有类似的解决方法,您只能将其用于最后一个参数.

Unfortunately there is no similar workaround for return types, and you can only use it for the last argument.

这篇关于PHP 7 中的类型提示 - 对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆