php-foreach-instanceof-rfc

Given collection:

$collection = [
  new \stdClass(),
  new ClassName(),
  new class extends ClassName() {},
  10,
  '10',
  'string value',
];

Class or interface name filter

foreach ($collection as ClassName $val) {
}

is equivalent of:

foreach ($collection as $val) {
  if ($val instanceof ClassName) {
  }
}

Scalar type filter

foreach ($collection as int $val) {
}

is equivalent of:

foreach ($collection as $val) {
  if (is_int($val)) {
  }
}

or

foreach ($collection as $val) {
  $val = (int)$val;
}

Key type filter

foreach ($collection as int $key => $val) {
}
foreach ($collection as int $key => ClassName $val) {
}

Support for union types

foreach ($collection as ClassName|int $val) {
}

With unpacking

foreach ($collection as array list($a, $v)) {
}
foreach ($collection as $key => void) {
}
foreach ($collection as int $key => void) {
}