Typed Array

By • Kartoffeljunge •

<?php

class User {}

function handle(User ...$userList) {
    print count($userList)." users
";
    /** ... */
}

$bunchOfUsers = [
    new User(),
    new User(),
    ];

echo handle(...$bunchOfUsers);

$bunchOfUsers[] = 1;
echo handle(...$bunchOfUsers); // error!$bunchOfUsers

It is possible to set the type of all elements in a variadic: this is equivalent of passing an argument of type array<User>. That way, all the elements in the array must have the expected type. Use union-type to make have several distinct types.

See Also

PHP Features

Last updated: 14 July 2026