Zaffar Saffee Zaffar Saffee - 2 years ago 66
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:


is of
@var List<FeeComponent> ItemFeeList

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

Answer Source

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.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download