yiisoft/yii2 · error · UnknownMethodException

Calling unknown method: {class}::{name}()

Error message

Calling unknown method: {class}::{name}()

What it means

For Components, __call() first scans attached behaviors and executes the first one that has the method; only when no behavior provides it does it throw UnknownMethodException('Calling unknown method: C::name()'). On a Component this error therefore means neither the class itself nor any attached behavior implements the method - and the most common real cause is an expected behavior that was never attached.

Source

Thrown at framework/base/Component.php:316

     * This method will check if any attached behavior has
     * the named method and will execute it if available.
     *
     * 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
     * @return mixed the method return value
     * @throws UnknownMethodException when calling unknown method
     */
    public function __call($name, $params)
    {
        $this->ensureBehaviors();
        foreach ($this->_behaviors as $object) {
            if ($object->hasMethod($name)) {
                return call_user_func_array([$object, $name], $params);
            }
        }
        throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
    }

    /**
     * This method is called after the object is created by cloning an existing one.
     * It removes all behaviors because they are attached to the old object.
     */
    public function __clone()
    {
        $this->_events = [];
        $this->_eventWildcards = [];
        $this->_behaviors = null;
    }

    /**
     * Returns a value indicating whether a property is defined for this component.
     *
     * A property is defined if:
     *

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. Dump $model->getBehaviors() at runtime and compare with what you expect - an empty list or a missing entry is the usual answer
  2. Fix the behaviors() declaration, e.g. return ['slug' => ['class' => SlugBehavior::class]]
  3. If the behavior is attached elsewhere (module or event), ensure that code runs before the call
  4. Otherwise call the behavior instance directly or move the method onto the class

Example fix

// before
class Post extends \yii\db\ActiveRecord
{
    // behaviors() forgotten
}
$post->makeSlug(); // UnknownMethodException

// after
class Post extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return ['slug' => ['class' => \app\behaviors\SlugBehavior::class]];
    }
}
Defensive patterns

Strategy: type-guard

Validate before calling

if ($component->hasMethod('upload')) { // Component::hasMethod checks attached behaviors
    $component->upload();
} else {
    throw new \BadMethodCallException('UploadBehavior is not attached');
}

Type guard

function behaviorProvides(\yii\base\Component $component, string $method): bool
{
    $component->ensureBehaviors();
    return $component->hasMethod($method);
}

Try / catch

try {
    $model->makeSlug();
} catch (\yii\base\UnknownMethodException $e) {
    // behavior not attached - degrade gracefully or attach and retry
    $model->attachBehavior('slug', \app\behaviors\SlugBehavior::class);
    $model->makeSlug();
}

Prevention

When it happens

Trigger: Calling $model->upload() (provided by an UploadBehavior) when behaviors() contains a typo'd class name, a wrong config shape, or the behavior attaches conditionally (a when() callback returning false); calling behavior methods on a clone (behaviors were cleared); behavior attach delayed behind an event that has not fired yet.

Common situations: Slug/Timestamp/Upload behaviors configured per model; behavior config supplied by a module that differs between tests and production; refactors that moved behaviors() out of the model class.

Related errors


AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17). Data as JSON: /api/errors/4232b0873597359c. Report an issue: GitHub.