问题描述
也许我错过了一些东西,但是是否有任何选项来定义该函数应具有参数或返回用户对象的示例数组?
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?
推荐答案
不包括在内.
对于当前的数组实现,由于数组本身不包含任何类型信息,因此需要在运行时检查所有数组元素.
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" -有趣的是,不是因为性能问题事实证明这是轻率的,但是因为在如何正确实施方面没有达成共识.也有人反对说,如果没有标量类型提示,它是不完整的.如果您对整个讨论感兴趣,请在邮件列表档案中阅读 . a>.
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数组类型提示与带类型的数组一起将提供最大的好处,我很乐意看到它们的实现.
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中的类型提示-对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!