WissensdatenbankPrüfungszentrum
DEDeutsch
Sprache wählen13

CKTIS / PHP

BadMethodCallException

BadMethodCallException spezialisiert BadFunctionCallException für ungültige Methodenaufrufe.

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

Syntax

throw new BadMethodCallException("unknown method")

BadMethodCallException spezialisiert BadFunctionCallException für ungültige Methodenaufrufe. Ein Handler wie __call kann sie ausdrücklich für unbekannte Namen werfen. Eine fehlende Methode ohne magischen Handler löst dagegen einen Engine-Error aus, den das Abfangen der SPL-Ausnahme nicht erfasst.

class Palette {
    public function __call(string $name, array $args): mixed {
        if ($name === "red") { return "#f00"; }
        throw new BadMethodCallException($name);
    }
}
$p = new Palette(); echo $p->red(), PHP_EOL;
try { $p->blue(); } catch (BadMethodCallException $e) { echo get_class($e), " ", $e->getMessage(), PHP_EOL; }
try { (new stdClass())->blue(); } catch (Error $e) { echo get_class($e); }
#f00
BadMethodCallException blue
Error

Beispiele

<?php
class Palette {
    public function __call(string $name, array $args): mixed {
        if ($name === "red") { return "#f00"; }
        throw new BadMethodCallException($name);
    }
}
$p = new Palette(); echo $p->red(), PHP_EOL;
try { $p->blue(); } catch (BadMethodCallException $e) { echo get_class($e), " ", $e->getMessage(), PHP_EOL; }
try { (new stdClass())->blue(); } catch (Error $e) { echo get_class($e); }

Ergebnis

#f00
BadMethodCallException blue
Error

Quellen