对象数组的PHPDoc类型提示? [英] PHPDoc type hinting for array of objects?

查看:25
本文介绍了对象数组的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类型提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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