Zaffar Saffee Zaffar Saffee - 6 months ago 21
PHP Question

passing list <Class> $obj to a mthod in PHP / enabling Type hinting

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?

EDIT:

I am using PHPStorm V-9, and was integrating Laravel-Amazon-mws library
To enable type-hint / Intellicode / Available method list ( whatever you name it), I needed to mention the
variable
type. I fell in to this problem. I know
List
is not something available in PHP, but we often say
List
instead of
Array
because the logic is somewhat the same, store easily accessible multiple items (though implementation and techs differs).

So, my question was about type hinting, at the moment of asking this question, I admit, I failed to think of
array
instead of
List
...

I am neither a native English, nor an expert like most of the guys here..so, I invite everyone to edit this question in a way someone else can get his solution from this question

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