allowedClass)) { $this->throwInvalid($value); } } parent::__construct($input, $flags, $iterator_class); } /** * @param mixed $value */ public function append ($value) { if (!is_a($value, $this->allowedClass)) { $this->throwInvalid($value); } return parent::append($value); } /** * @param mixed $input * @return array the old array */ public function exchangeArray ($input) { foreach($input as $value) { if (!is_a($value, $this->allowedClass)) { $this->throwInvalid($value); } } return parent::exchangeArray($input); } /** * @param mixed $index * @param mixed $newval */ public function offsetSet ($index , $newval) { if (!is_a($newval, $this->allowedClass)) { $this->throwInvalid($newval); } return parent::offsetSet($index , $newval); } protected function throwInvalid($newval) { throw new \InvalidArgumentException("Can not add element of type '" . (is_object($newval) ? get_class($newval) : gettype($newval)) . "' to Collection of type '" . get_class($this) . "'"); } }