CKTIS / PHP
BadMethodCallException
BadMethodCallException specializes BadFunctionCallException for invalid method calls.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
throw new BadMethodCallException("unknown method")BadMethodCallException specializes BadFunctionCallException for invalid method calls. A dispatcher such as __call may explicitly throw it when a requested method is unknown. An ordinary undefined method without a magic handler produces an engine Error instead. Catching the SPL exception does not intercept that Error.
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
Examples
<?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); }
Result
#f00 BadMethodCallException blue Error