Yoav Kadosh Yoav Kadosh - 1 year ago 67
PHP Question

Type hinting - specify an array of objects

How can I specify the argument type as an array?
Say I have a class named 'Foo':

class Foo {}

and then I have a function that accepts that class type as an argument:

function getFoo(Foo $f) {}

When I pass in an array of 'Foo's I get an error, saying:

Catchable fatal error: Argument 1 passed to getFoo()
must be an instance of Foo, array given

Is there a way to overcome this issue? maybe something like

function getFoo(Foo $f[]) {}

Answer Source

If you want to ensure you are working with "Array of Foo" and you want to ensure methods receive "Array of Foo", you can:

class ArrayOfFoo extends ArrayObject {
    public function offsetSet($key, $val) {
        if ($val instanceof Foo) {
            return parent::offsetSet($key, $val);
        throw new InvalidArgumentException('Value must be a Foo');


function workWithFoo(ArrayOfFoo $foos) {
    foreach ($foos as $foo) {
        // etc.

$foos = new ArrayOfFoos();
$foos[] = new Foo();

The secret sauce is that you're defining a new "type" of "array of foo", then passing that "type" around using type hinting protection.

The Haldayne library handles the boilerplate for membership requirement checks if you don't want to roll your own:

class ArrayOfFoo extends \Haldayne\Boost\MapOfObjects {
    protected function allowed($value) { return $value instanceof Foo; }

(Full-disclosure, I'm the author of Haldayne.)