BadMethodCallException
PHPERRORCommonLogic

Method called that does not exist or with bad arguments

Quick Answer

Throw BadMethodCallException from __call() for truly undefined methods; use IDE inspections to catch call-site typos.

What this means

Thrown when a method does not exist or has been called with bad arguments via __call(). Signals a programming error.

Why it happens
  1. 1__call() receives an unrecognised method name
  2. 2Fluent interface method called in wrong order

Fix

Validate method name in __call

Validate method name in __call
public function __call(string $name, array $args): mixed {
    $allowed = ['save', 'update', 'delete'];
    if (!in_array($name, $allowed, true)) {
        throw new \BadMethodCallException("Unknown method: $name");
    }
    return $this->$name(...$args);
}

Why this works

Listing allowed methods makes the error message informative and prevents silent failures.

Code examples
Builder guardphp
class QueryBuilder {
    public function where(string $col, mixed $val): static { /* ... */ return $this; }

    public function __call(string $n, array $a): never {
        throw new \BadMethodCallException("$n() does not exist on QueryBuilder");
    }
}

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PHP errors