yiisoft/yii2 · error · UnknownMethodException
Calling unknown method: {class}::{name}()
Error message
Calling unknown method: {class}::{name}() What it means
When a method call finds no real method on a yii\base\BaseObject subclass, __call() throws UnknownMethodException('Calling unknown method: C::name()') unconditionally - at this base level there is no fallback (only Component adds behavior dispatch). It means the method name is wrong, or the method lives on another class or on a behavior that the plain BaseObject does not support.
Source
Thrown at framework/base/BaseObject.php:223
$this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
}
}
/**
* Calls the named method which is not a class method.
*
* Do not call this method directly as it is a PHP magic method that
* will be implicitly called when an unknown method is being invoked.
* @param string $name the method name
* @param array $params method parameters
* @throws UnknownMethodException when calling unknown method
* @return mixed the method return value
*/
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
/**
* Returns a value indicating whether a property is defined.
*
* A property is defined if:
*
* - the class has a getter or setter method associated with the specified name
* (in this case, property name is case-insensitive);
* - the class has a member variable with the specified name (when `$checkVars` is true);
*
* @param string $name the property name
* @param bool $checkVars whether to treat member variables as properties
* @return bool whether the property is defined
* @see canGetProperty()
* @see canSetProperty()
*/
public function hasProperty($name, $checkVars = true)View on GitHub (pinned to 66f00d18a2)
Solutions
- Open the class named in the message and find the real method name (IDE go-to-definition on properly typed variables)
- If the method belongs to a related object, call it there, following the object chain the docs use
- For optional features guard the call with $obj->hasMethod($name) first
Example fix
// before $users = User::find()->whre(['status' => 1])->all(); // after $users = User::find()->where(['status' => 1])->all();
Defensive patterns
Strategy: type-guard
Validate before calling
if ($object->hasMethod($method)) {
$result = $object->$method($args);
} else {
throw new \BadMethodCallException(get_class($object) . "::{$method}() does not exist");
} Type guard
function supportsMethod(\yii\base\BaseObject $object, string $method): bool
{
return $object->hasMethod($method);
} Try / catch
try {
$result = $object->$method();
} catch (\yii\base\UnknownMethodException $e) {
Yii::warning($e->getMessage());
$result = $fallback;
} Prevention
- Type-hint variables so unknown methods surface as static-analysis errors
- For string-built method names, whitelist them against hasMethod()
- After upgrades, grep for removed/renamed methods listed in the CHANGELOG
When it happens
Trigger: Typos ($model->savve()); calling a method that exists on a related class (Query vs ActiveQuery) or on a behavior while the object is a plain BaseObject; methods removed or renamed between Yii 2.0.x releases; dynamic dispatch built from string method names.
Common situations: Version drift after composer updates; refactor drift between classes with similar APIs; string-built method names from user input or config.
Related errors
- Getting unknown property: {class}::{name}
- Setting unknown property: {class}::{name}
- Calling unknown method: {class}::{name}()
- 'Getting write-only property: ' . get_class($this) . '::' .
- Setting read-only property: {class}::{name}
AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17).
Data as JSON: /api/errors/d02ce8f6b90e27ac.
Report an issue: GitHub.