PHPDoc type hinting for array of objects?(对象数组的PHPDoc类型提示?)
问题描述
因此,在 PHPDoc 中,可以在成员变量声明上方指定 @var 以提示其类型.然后是 IDE,例如.PHPEd 将知道它正在使用什么类型的对象,并将能够为该变量提供代码洞察力.
So, in PHPDoc one can specify @var above the member variable declaration to hint at its type. Then an IDE, for ex. PHPEd, will know what type of object it's working with and will be able to provide a code insight for that variable.
<?php
class Test
{
/** @var SomeObj */
private $someObjInstance;
}
?>
这很有效,直到我需要对一组对象执行相同的操作,以便稍后在遍历这些对象时获得适当的提示.
This works great until I need to do the same to an array of objects to be able to get a proper hint when I iterate through those objects later on.
那么,有没有办法声明一个PHPDoc标签来指定成员变量是一个SomeObj的数组呢?@var 数组是不够的,例如 @var array(SomeObj) 好像不成立.
So, is there a way to declare a PHPDoc tag to specify that the member variable is an array of SomeObjs? @var array is not enough, and @var array(SomeObj) doesn't seem to be valid, for example.
推荐答案
使用:
/* @var $objs Test[] */
foreach ($objs as $obj) {
// Typehinting will occur after typing $obj->
}
当键入提示内联变量时,以及
when typehinting inline variables, and
class A {
/** @var Test[] */
private $items;
}
用于类属性.
09 年 PHPDoc(以及 Zend Studio 和 Netbeans 等 IDE)没有该选项时的上一个答案:
你能做的就是说,
foreach ($Objs as $Obj)
{
/* @var $Obj Test */
// You should be able to get hinting after the preceding line if you type $Obj->
}
我在 Zend Studio 中经常这样做.不知道其他编辑器,但它应该可以工作.
I do that a lot in Zend Studio. Don't know about other editors, but it ought to work.
这篇关于对象数组的PHPDoc类型提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:对象数组的PHPDoc类型提示?
基础教程推荐
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
