Baza wiedzyCentrum egzaminacyjne
PLPolski
Wybierz język13

CKTIS / PHP

ArgumentCountError

ArgumentCountError dziedziczy po TypeError.

Wersja: PHP 8.4; repository runtime, production runtime not attested

Składnia

try { strlen("a", "b"); } catch (ArgumentCountError $e) { /* ... */ }

ArgumentCountError dziedziczy po TypeError. W PHP 8.4 brak wymaganego argumentu funkcji użytkownika albo nadmiar pozycyjnych argumentów niewariadycznej funkcji wbudowanej powoduje ten błąd. Funkcja użytkownika może natomiast odczytać dodatkowe argumenty pozycyjne przez func_get_args. Przy braku argumentu ciało first nie zostaje wykonane.

function first($value) { return count(func_get_args()); }
foreach ([fn() => first(), fn() => strlen("x", "y")] as $call) {
    try { $call(); } catch (ArgumentCountError $e) {
        echo get_class($e), " ", ($e instanceof TypeError ? "TypeError" : "other"), PHP_EOL;
    }
}
echo first("A", "B");
ArgumentCountError TypeError
ArgumentCountError TypeError
2

Przykłady

<?php
function first($value) { return count(func_get_args()); }
foreach ([fn() => first(), fn() => strlen("x", "y")] as $call) {
    try { $call(); } catch (ArgumentCountError $e) {
        echo get_class($e), " ", ($e instanceof TypeError ? "TypeError" : "other"), PHP_EOL;
    }
}
echo first("A", "B");

Wynik

ArgumentCountError TypeError
ArgumentCountError TypeError
2

Źródła