Zaffar Saffee Zaffar Saffee - 5 months ago 14x
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:


is of
@var List<FeeComponent> ItemFeeList

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


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
type. I fell in to this problem. I know
is not something available in PHP, but we often say
instead of
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
instead of

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


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.