Search code examples
phparraystype-hinting

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[]) {}

Solution

  • 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');
        }
    }
    

    then:

    function workWithFoo(ArrayOfFoo $foos) {
        foreach ($foos as $foo) {
            // etc.
        }
    }
    
    $foos = new ArrayOfFoos();
    $foos[] = new Foo();
    workWithFoo($foos);
    

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


    Historical note: the Array Of RFC proposed this feature back in 2014. The RFC was declined with 4 yay and 16 nay. The concept recently reappeared on the internals list, but the complaints have been much the same as levied against the original RFC: adding this check would significantly affect performance.