Type hinting - specify an array of objects

前端 未结 5 1917
不思量自难忘°
不思量自难忘° 2020-12-10 10:23

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 tha

5条回答
  •  爱一瞬间的悲伤
    2020-12-10 11:13

    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.

提交回复
热议问题