<?php class A {} class B {} class C extends A {} $tests = [ 1 => [new A(), new A()], // succeeds 2 => [new A(), new B()], // fails 3 => [new C(), new C()], // succeeds ]; echo 'TEST 1:' . PHP_EOL; foreach ($tests as $index => $collection) { try { array_map(static function (A $object): void {}, $collection); } catch (\TypeError $e) { echo sprintf('Index %d failed, because it doesn\'t contain instances of A only.' . PHP_EOL, $index); } } echo PHP_EOL; echo 'TEST 2:' . PHP_EOL; foreach ($tests as $index => $collection) { try { array_map(static function (C $object): void {}, $collection); } catch (\TypeError $e) { echo sprintf('Index %d failed, because it doesn\'t contain instances of C only.' . PHP_EOL, $index); } }
You have javascript disabled. You will not be able to edit any code.