KennisbankExamencentrum
NL · NLNederlands (Nederland)
Taal kiezen13

CKTIS / PHP

ArgumentCountError

ArgumentCountError erft van TypeError.

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

Syntax

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

ArgumentCountError erft van TypeError. In PHP 8.4 treedt deze fout op bij een ontbrekend verplicht argument van een gebruikersfunctie of te veel positieargumenten voor een niet-variadische ingebouwde functie. Een gebruikersfunctie kan extra positieargumenten lezen met func_get_args. Zonder het verplichte argument draait de inhoud van first niet.

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

Voorbeelden

<?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");

Resultaat

ArgumentCountError TypeError
ArgumentCountError TypeError
2

Bronnen