1

i would like to do something like this:

class CCC {}  
function abc(array of CCC $variable)

Is this possible in php?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
Benas Radzevicius
  • 279
  • 1
  • 3
  • 17
  • Partial match: [Type check in all array elements](http://stackoverflow.com/questions/2323013/type-check-in-all-array-elements) – Leigh Aug 15 '12 at 12:32
  • ... and the long answer depends on why do you need this. If it'd be sufficient to check arguments in runtime, deceze's answer is right for you. But if what you need is IDE assistance, I suggest checking [this thread](http://stackoverflow.com/questions/778564/phpdoc-type-hinting-for-array-of-objects) for plenty of helpful hints. ) – raina77ow Aug 15 '12 at 12:39

1 Answers1

6

No.

You'll need to manually check:

function foo(array $arr) {
    if (array_filter($arr, function ($i) { return !($i instanceof CCC); })) {
        throw new InvalidArgumentException('Array must contain instances of CCC');
    }

    ...
}
deceze
  • 510,633
  • 85
  • 743
  • 889