Songo Songo - 3 months ago 10
PHP Question

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
.

Notes:


  • Supplier is a class which has a public method getSupplierName().

  • I'm using Netbeans IDE.


Answer
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.
        }
    }
}