KennisbankExamencentrum
NL · NLNederlands (Nederland)
Taal kiezen13

CKTIS / PHP

BadMethodCallException

BadMethodCallException specialiseert BadFunctionCallException voor ongeldige methodeaanroepen.

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

Syntax

throw new BadMethodCallException("unknown method")

BadMethodCallException specialiseert BadFunctionCallException voor ongeldige methodeaanroepen. Een afhandelaar als __call kan haar expliciet werpen bij een onbekende naam. Een ontbrekende methode zonder magische afhandelaar geeft een Error van de engine die buiten deze SPL-uitzondering valt.

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

Voorbeelden

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

Resultaat

#f00
BadMethodCallException blue
Error

Bronnen