Zaffar Saffee Zaffar Saffee - 6 months ago 14
PHP Question

passing list <Class> $obj to a mthod in PHP

Here is my method signature:

protected function updateFeeComponent(\FeeComponent $FeeList, $shipment_id, $item_id)
{
foreach($FeeList as $fee)
{
//other stuff
}
}


and I am calling it as:

$this->updateFeeComponent($shipmentFeeList,$event_id,$item_id);


where
$shipmentFeeList
is of
@var List<FeeComponent> ItemFeeList
.

How can I pass LIST of Custom object as a parameter to a function?

Answer

As far as I am aware, the closest you can get to what you want is something like:

/**
 * @param FeeComponent[] $FeeList
 */
protected function updateFeeComponent(array $FeeList) {
    // ...
}

Where you annotate with PHPDoc the type for $FeeList as being an array of FeeComponent and use PHP type hinting to specify the required type as being an array.

In an IDE that supports reading PHPDoc like PHPStorm, this will treat each element in the input array correctly as a FeeComponent and provide valid hinting e.g.

foreach ($FeeList as $fee) {
    $fee-> // Be provided with properties and methods of FeeComponent
           // in hints here.
}