我在数组中有一组自定义对象(Podcast)。
当我使用foreach循环遍历此集合时,我没有对包含从集合中拉出的对象的变量进行代码完成(例如在 C#/VisualStudio 中)。
有没有办法给 PHP 一个类型提示,以便 Eclipse 知道从集合中拉出的对象的类型,以便它可以在智能感知中向我显示该对象的方法?
<?php
$podcasts = new Podcasts();
echo $podcasts->getListHtml();
class Podcasts {
private $collection = array();
function __construct() {
$this->collection[] = new Podcast('This is the first one');
$this->collection[] = new Podcast('This is the second one');
$this->collection[] = new Podcast('This is the third one');
}
public function getListHtml() {
$r = '';
if(count($this->collection) > 0) {
$r .= '<ul>';
foreach($this->collection as $podcast) {
$r .= '<li>' . $podcast->getTitle() . '</li>';
}
$r .= '</ul>';
}
return $r;
}
}
class Podcast {
private $title;
public function getTitle() { return $this->title; }
public function setTitle($value) { $this->title = $value;}
function __construct($title) {
$this->title = $title;
}
}
?>
附录
谢谢,Fanis,我更新了我的 FOREACH 模板以自动包含该行:
if(count(${lines}) > 0) {
foreach(${lines} as ${line}) {
/* @var $$${var} ${Type} */
}
}