对象数组的PHPDoc类型提示?

PHPDoc type hinting for array of objects?(对象数组的PHPDoc类型提示?)
本文介绍了对象数组的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类型提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)