Base de connaissancesCentre d’examen
FRFrançais
Choisir la langue13

CKTIS / PHP

ArgumentCountError

ArgumentCountError dérive de TypeError.

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

Syntaxe

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

ArgumentCountError dérive de TypeError. En PHP 8.4 un argument requis absent d’une fonction utilisateur ou trop d’arguments positionnels pour une fonction interne non variadique provoque cette erreur. Une fonction utilisateur peut lire les arguments positionnels supplémentaires avec func_get_args. Sans argument requis, le corps de first ne s’exécute pas.

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

Exemples

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

Résultat

ArgumentCountError TypeError
ArgumentCountError TypeError
2

Sources