[英]Auto complete for a variable inside a foreach
I have the following code: 我有以下代码:
class Orders{
/**
*
* @var Supplier
*/
private $suppliers; //Array of Supplier
function loopAllSuppliers(){
foreach($this->suppliers as $supplier){
$supplier->/*no suggestion*/ //Can't get the method's to show here
$this->suppliers->getSupplierName(); //methods in class Supplier show normally here
}
}
}
The problem is easy. 问题很简单。 I just want to be able to declare a type for my variable
$supplier
like how I did it with $suppliers
. 我只是想能够为我的变量
$supplier
声明一个类型,就像我用$suppliers
做的那样。
Notes: 笔记:
class Orders{
/**
*
* @var Supplier
*/
private $suppliers;
function loopAllSuppliers(){
foreach($this->suppliers as $supplier){ /* @var $supplier Supplier */
//Must declare the type again inside the foreach as Netbeans doesn't support
// defining variable as arrays in doc blocks, yet.
}
}
}
This should work: 这应该工作:
class Orders
{
/**
* @var Supplier[]
*/
private $suppliers;
public function loopAllSuppliers()
{
foreach($this->suppliers as $supplier) {
}
}
}
For me this not work: 对我来说这不起作用:
foreach ($suppliers as /* @var $supplier Supplier */ $supplier) {
$supplier->/*should have suggestions*/
}
my solution: 我的解决方案
foreach ($suppliers as $supplier) {
if($suppliers instancof Supplier) {
$supplier->
}
}
try this if $this->suppliers is an array: 如果$ this-> suppliers是一个数组,请尝试这个:
function loopAllSuppliers(){
foreach($this->suppliers as $key => $supplier){
$supplier->/*no suggestion*/ //Can't get the method's to show here
$this->suppliers[$key]->getSupplierName(); //should work
$this->suppliers->getSupplierName(); //methods in class Supplier show normally here
}
}
You can achieve this(in netbeans) by doing the following: 您可以通过执行以下操作来实现此目标(在netbeans中):
foreach ($suppliers as /* @var $supplier Supplier */ $supplier) { $supplier->/*should have suggestions*/ }
With PHP7 and PHPStorm I can use: 使用PHP7和PHPStorm,我可以使用:
use path/to/Supplier; //needs the path for your class Supplier
foreach ($suppliers as /* @var $supplier Supplier */ $supplier) {
echo $supplier->getName(); //Output each Name
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.