I want to
return array(new Foo(), new Bar());
有没有一种方法可以对此进行类型提示?
I want to
return array(new Foo(), new Bar());
简短的回答是不行。
稍微详细一点的答案是,您可以创建自己的值对象用作提示,但这意味着您需要返回一个对象而不是数组。
class Foo {};
class Bar {};
class Baz {
private $foo;
private $bar;
public function __construct(Bar $bar, Foo $foo) {
$this->bar = $bar;
$this->foo = $foo;
}
public function getFoo() : Foo {
return $this->foo;
}
public function getBar() : Bar {
return $this->bar;
}
}
function myFn() : Baz {
return new Baz(new Bar(), new Foo());
}
$myObj = myFn();
var_dump($myObj);
http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration
http://php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration
这在phpstorm中有效(当然不需要指定元素的顺序):
/**
* @return Foo[]|Bar[]
*/