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

查看:755
本文介绍了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标记来指定成员变量是一个<$ c的数组$ C> someObj中 S' @var 数组不够, @var数组(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.

推荐答案

你能做的最好就是说,

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天全站免登陆