因此,在PHPDoc中,可以在成员变量声明上方指定@var,以提示其类型。然后一个IDE,例如PHPEd,将知道它正在处理的对象类型,并能够为该变量提供代码洞察力。

<?php
  class Test
  {
    /** @var SomeObj */
    private $someObjInstance;
  }
?>

这很好,直到我需要对对象数组做同样的事情,以便在以后迭代这些对象时能够得到适当的提示。

那么,是否有一种方法来声明PHPDoc标签,以指定成员变量是SomeObjs的数组?@var数组是不够的,@var数组(SomeObj)似乎是无效的,例如。


当前回答

<?php foreach($this->models as /** @var Model_Object_WheelModel */ $model): ?>
    <?php
    // Type hinting now works:
    $model->getImage();
    ?>
<?php endforeach; ?>

其他回答

PSR-5: PHPDoc提出了一种泛型风格的表示法。

语法

Type[]
Type<Type>
Type<Type[, Type]...>
Type<Type[|Type]...>

集合中的值甚至可以是另一个数组,甚至是另一个集合。

Type<Type<Type>>
Type<Type<Type[, Type]...>>
Type<Type<Type[|Type]...>>

例子

<?php

$x = [new Name()];
/* @var $x Name[] */

$y = new Collection([new Name()]);
/* @var $y Collection<Name> */

$a = new Collection(); 
$a[] = new Model_User(); 
$a->resetChanges(); 
$a[0]->name = "George"; 
$a->echoChanges();
/* @var $a Collection<Model_User> */

注意:如果你期望IDE来做代码辅助,那么IDE是否支持PHPDoc泛型风格的集合符号就是另一个问题了。

从我对这个问题的回答来看。

在NetBeans 7.0(可能更低)中,你可以声明返回类型为“数组与文本对象”,就像@return Text和代码提示一样:

编辑:用@Bob Fanger的建议更新了示例

/**
 * get all Tests
 *
 * @return Test|Array $tests
 */
public function getAllTexts(){
    return array(new Test(), new Test());
}

使用它:

$tests =  $controller->getAllTests();
//$tests->         //codehinting works!
//$tests[0]->      //codehinting works!

foreach($tests as $text){
    //$test->      //codehinting works!
}

这并不完美,但总比“混合”要好,因为“混合”没有任何价值。

缺点是你可以将数组作为文本对象,这会抛出错误。

正如DanielaWaranie在她的回答中提到的——当你在$collectionObject中迭代$items时,有一种方法可以指定$item的类型:将@return MyEntitiesClassName添加到current()以及返回值的Iterator和arrayaccess方法的其余部分。

繁荣!不需要在/** @var SomeObj[] $collectionObj */ over foreach中,与收集对象一起工作,不需要使用特定的方法@return SomeObj[]返回收集。

我怀疑不是所有的IDE都支持它,但它在PhpStorm中工作得很好,这让我更高兴。

例子:

class MyCollection implements Countable, Iterator, ArrayAccess {

    /**
     * @return User
     */
    public function current() {
        return $this->items[$this->cursor];
    }

    //... implement rest of the required `interface` methods and your custom
}

有什么有用的我要添加张贴这个答案

在我的情况下,current()和其余的接口方法是在抽象集合类中实现的,我不知道什么样的实体最终将存储在集合中。

这里有一个技巧:不要在抽象类中指定返回类型,而是在特定的集合类的描述中使用PhpDoc指令@method。

例子:

class User {

    function printLogin() {
        echo $this->login;
    }

}

abstract class MyCollection implements Countable, Iterator, ArrayAccess {

    protected $items = [];

    public function current() {
        return $this->items[$this->cursor];
    }

    //... implement rest of the required `interface` methods and your custom
    //... abstract methods which will be shared among child-classes
}

/**
 * @method User current()
 * ...rest of methods (for ArrayAccess) if needed
 */
class UserCollection extends MyCollection {

    function add(User $user) {
        $this->items[] = $user;
    }

    // User collection specific methods...

}

现在,类的使用:

$collection = new UserCollection();
$collection->add(new User(1));
$collection->add(new User(2));
$collection->add(new User(3));

foreach ($collection as $user) {
    // IDE should `recognize` method `printLogin()` here!
    $user->printLogin();
}

再说一次:我怀疑不是所有的IDE都支持它,但是PhpStorm支持。试试你的,在结果中发表评论!

在JetBrains的PhpStorm IDE中,你可以使用/** @var SomeObj[] */,例如:

/**
 * @return SomeObj[]
 */
function getSomeObjects() {...}

phpdoc文档推荐这个方法:

指定包含单个类型时,type定义通知读者每个数组元素的类型。对于给定的数组,只期望有一个Type作为元素。 示例:@return int[]

问题是@var只能表示一个类型-不包含复杂的公式。如果你有一个语法的“数组的Foo”,为什么停在那里,不添加一个语法的数组的数组,其中包含2个Foo和3个Bar”?我知道元素列表可能比这更通用,但这是一个滑坡。

就我个人而言,我有时会使用@var Foo[]来表示“Foo的数组”,但IDE不支持。