我能做些什么来让PhpStorm(2016.1)识别"new X()"
范围之外的类型吗?
SomeClass.php:
class SomeClass
{
public function DoMagic()
{
echo "doing magic";
}
}
DummyClass.php:
class DummyClass
{
protected $mParamsList;
function __construct()
{
$this->mParamsList = array();
}
public function InitParamsList()
{
$this->mParamsList[] = new SomeClass();
}
public function GetParamsList()
{
return $this->mParamsList;
}
}
UserClass.php - 没有建议:
class UserClass
{
public function DoMagic()
{
$dummy2 = new DummyClass();
$params = $dummy2->GetParamsList();
foreach ($params as $param)
{
$param-> * nothing happens *
}
}
}
?>
我发现添加此hack有效,但使用它会让人感到沮丧:
if(false) { $ param = new SomeClass(); }
所以完整的工作示例是:
class UserClass
{
public function DoMagic()
{
$dummy = new DummyClass();
$params = $dummy->GetParamsList();
foreach ($params as $param)
{
if (false)
{
$param = new SomeClass();
}
$param-> * suggestions pop up *
}
}
}
答案 0 :(得分:4)
您应该在功能之前使用doc-type注释:
/**
* @return \MyObject
*/
public function GetMyObject()
{
return new MyObject();
}