PHPDoc:嵌套数组中的Typehint(例如2维) [英] PHPDoc: Typehint in nested arrays (with e.g. 2 dimensions)

查看:81
本文介绍了PHPDoc:嵌套数组中的Typehint(例如2维)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在正确的方法来记录另一个维度内的数组中的值/对象?

Is there a correct way to document values/objects in arrays which are within another dimension?

通常,数组将按以下方式处理:

Normally an array will be handled like this:

/** @var ClassName[] $Array */
$Array = array( $InstanceOfClassName,.. )

但是我需要这样的东西:

But i need something like this:

/** @var ClassName[][] $Array */
$Array = array( 0 => array( $InstanceOfClassName,.. ) )

这显然不起作用,那么正确的PHPDoc表示法是什么?

This is obviously not working, so what is the correct PHPDoc notation?

推荐答案

首先,了解@var的用法不是标准的phpDocumentor规范.这是不同的IDE尝试使局部变量自动补全"成为可能的几种不同方法之一.我知道Eclipse使用/* @var $varName \ClassName */格式.因此,请紧记其他答案/建议.

First, understand that this usage of @var is not standard phpDocumentor spec. It is one of several different ways that different IDEs have tried to make "autocompletion for local variables" possible. I know that Eclipse uses the format /* @var $varName \ClassName */. So keep that in mind as other answers/suggestions come in.

我可以看到在二维数组中利用此IDE自动完成功能的唯一方法是稍后在读取第一维时使用另一个@var,尽管这确实要求进入变量本身:

The only way I can see to leverage this IDE autocompletion hack with your two dimensional array is to use another @var later on when you are reading the first dimension out, though this does require that to go into a variable itself:

/* @var $outer array */
$outer = array( 0 => array($InstanceOfClassName,...));

/* @var $inner ClassName[] */
$inner = $outer[0];

$inner[0]-> (在这里期待ClassName方法的自动完成)

现在,这对于自动完成有多么有用取决于您的IDE的构建方式.一些IDE可能知道ClassName []语法并推断出从$ inner中提取的元素是ClassName,因此可以显示其方法.但是,我不能说我曾经见过任何 IDE.最多,看到IDE只是以其最基本的形式(/* @var $var ClassName */)可用@var hack一直很费力.

Now again, how useful this can be for autocompletion depends on how your IDE has built it. Some IDEs might know that ClassName[] syntax and deduce that an element pulled from $inner is a ClassName, and therefore it can show its methods. However, I cannot say I've seen any IDE do that yet. At most, it's been a stretch to see IDEs simply have the @var hack available in its most basic form (/* @var $var ClassName */).

TL; DR:您的里程可能会有所不同,只是试图获得您所追求的一半:-)

TL;DR: Your mileage may vary in just trying to get half of what you're after :-)

这篇关于PHPDoc:嵌套数组中的Typehint(例如2维)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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