<?php
class User { public function __construct(public int $id) {} }
class Admin { public function __construct(public int $id) {} }
/**
* @param User[] $users
*/
function deleteUsers(array $users)
{
$typedArray = (fn (User ...$users) => $users)(...$users);
foreach ($typedArray as $user) {
var_dump($user->id);
}
}
deleteUsers([new User(1), new User(2), new User(3)]);
deleteUsers([new User(4), new Admin(5), new User(6)]);
int(1)
int(2)
int(3)
Fatal error: Uncaught TypeError: {closure}(): Argument #2 must be of type User, Admin given, called in /in/fjrcM on line 11 and defined in /in/fjrcM:11
Stack trace:
#0 /in/fjrcM(11): {closure}(Object(User), Object(Admin), Object(User))
#1 /in/fjrcM(19): deleteUsers(Array)
#2 {main}
thrown in /in/fjrcM on line 11
Process exited with code 255.