Intereting Posts

PHP-массив не-примитивного класса в качестве параметра функции

Мне интересно (если возможно), как вы объявите массив не примитивного класса в качестве параметра функции. Например

<?php class C {} function f(array C $c) { /* use $c[1], $c[2]... */ } 

Основной факт – в настоящее время вы не можете вводить аргумент подсказки как array of something .

Таким образом, вы можете:

 // just a function with some argument, // you have to check whether it is array // and whether each item in this array has type `C` function f($c) {} // function, which argument MUST be array. // if it is not array - error happens // you still have to check whether // each item in this array has type `C` function f(array $c) {} // function, which argument of type CCollection // So you have to define some class CCollection // object of this class can store only `C` objects function f(CCollection $c) {} // class CCollection can be something like class CCollection { private $storage = []; function addItem(C $item) { $this->storage[] = $item; } function getItems() { return $this->storage; } }