Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / PHP

ArgumentCountError

ArgumentCountError deriva de TypeError.

Versión: PHP 8.4; repository runtime, production runtime not attested

Sintaxis

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

ArgumentCountError deriva de TypeError. En PHP 8.4 se produce al faltar un argumento obligatorio de una función de usuario o sobrar argumentos posicionales en una función interna no variádica. Una función de usuario puede leer argumentos posicionales extra con func_get_args. Sin el argumento requerido no se ejecuta el cuerpo de first.

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

Ejemplos

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

Resultado

ArgumentCountError TypeError
ArgumentCountError TypeError
2

Fuentes